AI: add agent.assert_tokens() for accumulated token limits - #200
Merged
Conversation
agent.assert_tokens(lambda x: x.where('input', '<=', 5000).where('output', '<=', 5000))
Tokens accumulate across every prompt()/stream() in a session (summing each
ai message's token uses from the transcript, with a usage fallback), and the
fluent TokenQuery evaluates where-clauses over input/output/cache/total. Works
on both live-record and replay; to_response() now carries the transcript so
replayed turns expose their per-message uses.
CI runs 'ruff format --check .' repo-wide; these two files (carried in from #199) were unformatted and failed the check.
The Like model's MorphTo relationship is named 'record'; the tests referenced
a nonexistent 'like.log' attribute, which returned None and failed to await /
assert. Aligns with the eager-load case that already uses with_('record').
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #199 (which is already merged into
ai-package-fix). Adds a fluent assertion for the accumulated token usage of a recorded/faked agent session:Behavior
aimessage's tokenusesacross all turns (and every ai message within a turn), not just the last response.TokenQuery—.where(field, op, value), chainable; all clauses must hold.input/output/cache/total<=,>=,<,>,==,!=recording.to_response()now carries thetranscript, so replayed turns still expose their per-messageuses. Falls back to the response's summaryusagefor legacy flat cassettes.Token assertion failed: input=6000 is not <= 5000. Accumulated tokens: {...}..fake()and.record()handles; totals reset with.reset().Changes
ai/recording.py:accumulate_uses()helper;to_response()carries the transcript.ai/testing.py:TokenQuery,assert_tokens(), per-turn accumulation (via the existing_remember/_remember_turnhooks), reset handling.tests/ai/test_assert_tokens.py(new, TDD): within-limit pass, over-limit fail, cache/total fields, accumulation on replay.example/agents/.../test_router_agent.py: demonstrates it end-to-end (accumulatedinput=171, output=33over the two turns).Testing
tests/ai/test_assert_tokens.py+test_recording.py: 16 passed.example/agentsrouter tests: 2 passed.Depends on / targets
ai-package-fix.