fix: count cached tokens in context-size and compaction baseline - #3886
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Issue: This changes the runtime value of the public read-only properties Suggestion: Add a release note documenting the behavior change on these public properties, and consider whether an |
|
Assessment: Comment / Request Changes Well-scoped fix with good cross-SDK parity and tests that assert the subset/disjoint/collapse invariant (as #3546 explicitly asked for). The main concern is that the convention-detection heuristic doesn't fully deliver the coverage the docstring claims. Review themes
Nice work routing both SDKs through a single shared helper and covering the invariant rather than field presence. |
|
Issue (follow-up to the review — the file isn't in this PR's diff so noting it here): the new Suggestion: When touching this area, update the |
|
Updated assessment (re-review of The Resolved
Verified locally: the telemetry Remaining items are minor and non-blocking: the |
|
Updated assessment (re-review of The new commit extends the cache-aware prompt sizing into the tracer, and it's cleanly done. Review notes
Remaining non-blocking items from earlier still stand: the |
…x-linting # Conflicts: # strands-py/tests/strands/telemetry/test_tracer.py
|
Re-review of This update is a merge from
The only open item remains the minor, non-blocking one from the last round: the |
|
Re-review of This commit is a clean refactor that also picks up prior feedback: What changed & verification
Only remaining non-blocking item: a release note for the public property / telemetry value change. Nice work tightening this up. |
|
I’m seeing a behavior after the recent telemetry changes that are causing an interoperability issue with AgentCore GenAI Observability. After the change, gen_ai.usage.input_tokens appears to represent the full prompt size, including cache-read and cache-write tokens, while the cache token counts are also emitted separately. For example: AgentCore GenAI Observability appears to interpret the full input_tokens value as normal billable input and then adds the cache-read/write costs separately, which results in apparent double-counting in the cost display. |
Description
Context-window sizing and proactive compaction size the prompt as
inputTokens + outputTokens. That undercounts cached tokens on providers where the cache read is reported additional toinputTokens(Bedrock Converse, Anthropic-direct): a 90k-token cache is currently underreported, so context utilization looks near-zero, proactive compaction never fires, and the run hits a hard context-window error instead of compacting.On providers that fold the cache read into
inputTokens(OpenAI, Gemini, LiteLLM) the same code path is already correct, so the fix handles both conventions without assuming either.Two existing read-only properties change value on cache-reporting providers:
AgentResult.context_size/AgentResult.projected_context_size(Python), and the equivalentcontextSize/projectedContextSize(TypeScript). On a disjoint-cache provider they now report the full prompt the model processed rather than the uncached remainder, e.g. a turn withinputTokens=10, cacheReadInputTokens=5848now reports5858instead of10.Related Issues
#3546
Documentation PR
N/A
Type of Change
Bug fix
Testing
hatch run prepareRan the Python telemetry, event-loop, and conversation-manager unit suites and the TypeScript meter, agent, and streaming suites; both cover the subset and disjoint conventions plus the no-cache collapse using the token counts captured in #3546. Python lint/type/complexity and TypeScript lint/type-check are clean.
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.