Skip to content

Context

The shared context stores metrics that are written to log storage each time a WorkflowRunner instance completes:

  1. Step, stage and workflow durations
  2. OpenAI token consumption rates
  3. Downloaded bytes
  4. And more

get_metrics

get_metrics()

Retrieve a deep copy of the current metrics dictionary.

Returns:

Type Description
Dict[str, Any]

The context dictionary.

init_metrics

init_metrics()

Reset the metrics context for a new workflow run.

log_metric

log_metric(path, value)

Log a metric using a dotted path for nested metrics, e.g., "stage1.step1.tokens". It will sum numeric values or append string values to the list if keys already exist.

When rendering metrics in the Hoppa web app workspace dashboard, the app expects to find certain keys in the metrics JSON. An example JSON structure is provided below to help you construct the right dot notation when creating new class methods or functions.

{
    "duration_sec": {
        "stages": {
            "Hoppa Connector": {
                "Establish Hoppa session connection and get document": 0.3196,
                "total": 0.8427
            },
            "File Preprocessing": {
                "Preprocess file": 0.3173,
                "total": 0.8587
            },
            "Classification": {
                "Generate document description": 4.3365,
                "Classify site using ISO4 Spatial Layout classifier": 10.5834,
                "total": 15.9178
            },
            "Search Terms": {
                "Run search terms": 2.4235,
                "total": 2.9494
            }
        },
        "total": 20.7674
    },
    "bytes": 186069,
    "file_type": "svg",
    "counts": {
        "document_characters": 186069,
        "chat": {
            "prompt_tokens": 23645,
            "completion_tokens": 611,
            "total_tokens": 24256
        }
        "descriptions": 1
        "description_characters": 1399
        "classifiers": 6
        "searchTerms": 6
        "tags": 2
    },
}

Parameters:

Name Type Description Default
path str

The dotted path to insert into the context dictionary

required
value Any

The value to be inserted

required

merge_metrics_into_main

merge_metrics_into_main(thread_metrics)

Merge collected metrics from a thread into the main metrics context. Supports nested dictionaries and sums numeric values.

Parameters:

Name Type Description Default
thread_metrics dict

Dictionary of key-value pairs to insert. Expects pre-structured (nested) dictionaries, as opposed to dot notation.

required