Skip to content

AI: add agent.assert_tokens() for accumulated token limits - #200

Merged
tmgbedu merged 4 commits into
ai-package-fixfrom
task/ai-assert-tokens-1251
Jul 25, 2026
Merged

AI: add agent.assert_tokens() for accumulated token limits#200
tmgbedu merged 4 commits into
ai-package-fixfrom
task/ai-assert-tokens-1251

Conversation

@tmgbedu

@tmgbedu tmgbedu commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

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:

agent.assert_tokens(lambda x: (
    x.where("input", "<=", 5000).where("output", "<=", 5000)
))

Behavior

  • Accumulated across the session — sums every ai message's token uses across all turns (and every ai message within a turn), not just the last response.
  • Fluent TokenQuery.where(field, op, value), chainable; all clauses must hold.
    • Fields: input / output / cache / total
    • Operators: <=, >=, <, >, ==, !=
  • Live-record and replayrecording.to_response() now carries the transcript, so replayed turns still expose their per-message uses. Falls back to the response's summary usage for legacy flat cassettes.
  • Failure message reports the offending clause and the running totals, e.g. Token assertion failed: input=6000 is not <= 5000. Accumulated tokens: {...}.
  • Available on both .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_turn hooks), 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 (accumulated input=171, output=33 over the two turns).

Testing

  • tests/ai/test_assert_tokens.py + test_recording.py: 16 passed.
  • example/agents router tests: 2 passed.
  • Lint clean.

Depends on / targets ai-package-fix.

tmgbedu added 4 commits July 25, 2026 03:02
    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

codecov Bot commented Jul 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.87234% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...astapi_startkit/src/fastapi_startkit/ai/testing.py 97.29% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@tmgbedu
tmgbedu merged commit cb9dd29 into ai-package-fix Jul 25, 2026
6 checks passed
@tmgbedu
tmgbedu deleted the task/ai-assert-tokens-1251 branch July 25, 2026 19:50
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