-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add token usage metrics #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,22 @@ | ||
| import pytest | ||
| from pydantic_ai.exceptions import ModelHTTPError | ||
| from pydantic_ai.models.fallback import FallbackModel | ||
| from pydantic_ai.models.test import TestModel | ||
|
|
||
| from lorebinders.agent.factory import _is_moderation_error, create_agent | ||
| from lorebinders.models import AgentDeps, ExtractionResult | ||
| from lorebinders.agent.factory import ( | ||
| _is_moderation_error, | ||
| create_agent, | ||
| create_extraction_agent, | ||
| load_prompt_from_assets, | ||
| run_agent_async, | ||
| ) | ||
| from lorebinders.models import ( | ||
| AgentDeps, | ||
| ExtractionResult, | ||
| ObservationEvent, | ||
| ObservationType, | ||
| ) | ||
| from lorebinders.settings import get_settings | ||
|
|
||
|
|
||
| def test_is_moderation_error_true() -> None: | ||
|
|
@@ -40,3 +53,38 @@ def test_create_agent_with_fallback_wraps_in_fallback_model() -> None: | |
| fallback=fallback, | ||
| ) | ||
| assert isinstance(agent.model, FallbackModel) | ||
|
|
||
|
|
||
| @pytest.mark.anyio | ||
| async def test_run_agent_async_emits_metric_event() -> None: | ||
| """Test that run_agent_async emits a METRIC event with token counts.""" | ||
| observations: list[ObservationEvent] = [] | ||
|
|
||
| def on_observe(event: ObservationEvent) -> None: | ||
| observations.append(event) | ||
|
|
||
| agent = create_extraction_agent() | ||
| agent.model = TestModel() | ||
|
|
||
| deps = AgentDeps( | ||
| settings=get_settings(), | ||
| prompt_loader=load_prompt_from_assets, | ||
| ) | ||
|
|
||
| await run_agent_async( | ||
| agent, "test prompt", deps=deps, on_observe=on_observe | ||
| ) | ||
|
|
||
| metric_events = [ | ||
| 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["input_tokens"] >= 0 | ||
| assert meta["output_tokens"] >= 0 | ||
| assert meta["total_tokens"] >= 0 | ||
| assert meta["total_tokens"] == meta["input_tokens"] + meta["output_tokens"] | ||
|
Comment on lines
+82
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| assert "model" in meta | ||
Uh oh!
There was an error while loading. Please reload this page.