Skip to content

feat: add token usage metrics#1

Open
ashrobertsdragon wants to merge 2 commits into
mainfrom
feat/token-usage-metrics
Open

feat: add token usage metrics#1
ashrobertsdragon wants to merge 2 commits into
mainfrom
feat/token-usage-metrics

Conversation

@ashrobertsdragon
Copy link
Copy Markdown
Collaborator

@ashrobertsdragon ashrobertsdragon commented Apr 2, 2026

Summary of changes

This PR adds token usage tracking to the asynchronous agent execution flow. When an agent run completes successfully, it now extracts token statistics (input, output, and total) and emits them as a metric observation.

Key features/fixes

  • Token Usage Monitoring: In run_agent_async, the model's usage data is now captured and emitted using the METRIC observation type.
  • Enhanced Metadata: Emitted metrics include the specific model used, input tokens, output tokens, and the sum total.
  • Testing: Added a new unit test test_run_agent_async_emits_metric_event to verify that metric events are correctly triggered with valid token counts upon successful execution.

Breaking changes

None.

Summary by Sourcery

Track and emit token usage metrics after asynchronous agent runs and validate this behavior with a new unit test.

New Features:

  • Emit METRIC observation events containing token usage details (model, input tokens, output tokens, and total tokens) when an agent run completes successfully.

Tests:

  • Add an async unit test to verify that run_agent_async emits a single METRIC event with valid token counts and model metadata upon successful execution.

…ent run

Captures input_tokens, output_tokens, total_tokens, and model from the
pydantic-ai RunResult after every successful LLM call in run_agent_async,
emitting them via the existing on_observe callback as ObservationType.METRIC.
The package remains ignorant of user and run identity.
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Apr 2, 2026

Reviewer's Guide

Adds token usage metric emission to the async agent execution flow and introduces a unit test to verify that METRIC observations with token counts are produced on successful runs.

Sequence diagram for async agent run with token usage metric emission

sequenceDiagram
    actor Caller
    participant run_agent_async
    participant Model
    participant emit_observation
    participant Observer

    Caller->>run_agent_async: invoke(model, on_observe, ...)
    run_agent_async->>Model: execute_async()
    Model-->>run_agent_async: res(output, usage)

    run_agent_async->>emit_observation: emit_observation(on_observe, EVENT, agent, Agent run completed...)
    emit_observation-->>Observer: on_observe(EVENT, meta)

    run_agent_async->>run_agent_async: usage = res.usage()

    run_agent_async->>emit_observation: emit_observation(on_observe, METRIC, agent, Token usage for model, {model, input_tokens, output_tokens, total_tokens})
    emit_observation-->>Observer: on_observe(METRIC, metric_meta)

    run_agent_async-->>Caller: res.output
Loading

Flow diagram for run_agent_async with token usage metric

flowchart TD
    A[Start run_agent_async] --> B[Call model to execute asynchronously]
    B --> C[Receive res with output and usage]
    C --> D[Emit EVENT observation for successful agent run]
    D --> E["Extract usage = res.usage()"]
    E --> F[Compute total_tokens = input_tokens + output_tokens]
    F --> G[Emit METRIC observation with model and token counts]
    G --> H[Return res.output]
    H --> I[End]
Loading

File-Level Changes

Change Details Files
Emit token usage metrics after successful async agent runs.
  • After a successful run_agent_async call, retrieve usage statistics from the result object.
  • Emit a METRIC-type observation named 'agent' with a message describing token usage for the model.
  • Include model name, input_tokens, output_tokens, and total_tokens (sum of input and output) in the metric metadata.
src/lorebinders/agent/factory.py
Add test coverage to ensure METRIC events with token counts are emitted.
  • Import additional factory helpers and observation-related types needed to construct and inspect events.
  • Create an async test that wires an on_observe callback to collect ObservationEvents during run_agent_async.
  • Assert that exactly one METRIC event is emitted and that its metadata includes integer input_tokens, output_tokens, total_tokens equal to their sum, and model identifier.
tests/unit/agents/test_factory.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues, and left some high level feedback:

  • Consider guarding res.usage() in run_agent_async (e.g., handle None or missing attributes) so that a missing usage payload doesn't cause the whole agent run to fail when emitting metrics.
  • The test_run_agent_async_emits_metric_event assumes exactly one METRIC event, which could become brittle if additional metrics are emitted in the future; filtering by a more specific property (e.g., message or metadata key) would make the assertion more robust.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider guarding `res.usage()` in `run_agent_async` (e.g., handle `None` or missing attributes) so that a missing usage payload doesn't cause the whole agent run to fail when emitting metrics.
- The `test_run_agent_async_emits_metric_event` assumes exactly one `METRIC` event, which could become brittle if additional metrics are emitted in the future; filtering by a more specific property (e.g., message or metadata key) would make the assertion more robust.

## Individual Comments

### Comment 1
<location path="src/lorebinders/agent/factory.py" line_range="128-127" />
<code_context>
             f"Agent run completed with model {model}",
             meta,
         )
+        usage = res.usage()
+        emit_observation(
+            on_observe,
+            ObservationType.METRIC,
+            "agent",
+            f"Token usage for model {model}",
+            {
+                "model": model,
+                "input_tokens": usage.input_tokens,
+                "output_tokens": usage.output_tokens,
+                "total_tokens": usage.input_tokens + usage.output_tokens,
+            },
+        )
         return res.output
     except Exception as e:
</code_context>
<issue_to_address>
**issue (bug_risk):** Guard against `res.usage()` failures so they don't turn a successful agent run into an error.

Because `res.usage()` and metric emission are inside the main `try`, any error there will be caught by the outer `except` and incorrectly turn a successful agent run into a failure. To keep `run_agent_async`’s behavior unchanged, please wrap the usage/metric logic in its own `try/except` (ideally catching specific exceptions) and log a warning so usage collection remains best-effort and non-fatal.
</issue_to_address>

### Comment 2
<location path="tests/unit/agents/test_factory.py" line_range="82-86" />
<code_context>
+        o for o in observations if o.type == ObservationType.METRIC
+    ]
+    assert len(metric_events) == 1
+    meta = metric_events[0].metadata
+    assert isinstance(meta["input_tokens"], int)
+    assert isinstance(meta["output_tokens"], int)
+    assert isinstance(meta["total_tokens"], int)
+    assert meta["total_tokens"] == meta["input_tokens"] + meta["output_tokens"]
+    assert "model" in meta
</code_context>
<issue_to_address>
**suggestion (testing):** Strengthen assertions on token values by checking for non-negative counts.

Currently the test only checks that token counts are integers and that `total_tokens` equals the sum of input and output tokens. Please also assert that `input_tokens`, `output_tokens`, and `total_tokens` are `>= 0` so tests fail if negative token values are ever emitted.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/lorebinders/agent/factory.py
Comment on lines +82 to +86
meta = metric_events[0].metadata
assert isinstance(meta["input_tokens"], int)
assert isinstance(meta["output_tokens"], int)
assert isinstance(meta["total_tokens"], int)
assert meta["total_tokens"] == meta["input_tokens"] + meta["output_tokens"]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Strengthen assertions on token values by checking for non-negative counts.

Currently the test only checks that token counts are integers and that total_tokens equals the sum of input and output tokens. Please also assert that input_tokens, output_tokens, and total_tokens are >= 0 so tests fail if negative token values are ever emitted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant