Repository navigation
fix(llm): cache the conversation history on the Claude provider - #5071
Merged
Merged
Conversation
Every call to Claude re-billed the whole conversation as fresh input, so an agent loop paid full price for its growing history on each step. Only the tool schemas and system prompt were cached. Now each call caches its history and the next call reads it back. - A rolling cache_control breakpoint goes on the last block of the newest message that can carry one. String content becomes one text block; thinking blocks and empty text are skipped, and nothing is marked when no block qualifies. tool_result blocks can carry it. - _build_params is shared by the create and stream paths, so both get it. - The caller's messages are copied, never edited, so markers can't pile up across calls. - A request over Anthropic's limit of 4 breakpoints fails with an actionable ValueError. The provider places at most 3 itself. - The live integration test confirms call 2 reads call 1's history from cache. It skips without a real ANTHROPIC_API_KEY and is listed in the lane allowlist, since no test lane carries a billed key.
4 tasks
kovtcharov
pushed a commit
to kovtcharov/gaia
that referenced
this pull request
Oct 26, 2026
…e provider (amd#5072) With thinking enabled, a Claude tool loop in GAIA re-derived its plan on every call. The provider threw away each turn's thinking blocks, so the model never saw its own earlier reasoning and spent output tokens and time working it out again. Now each tool turn goes back exactly as Claude produced it. Thinking is left out only when the earlier conversation has changed, because Anthropic rejects a stale thinking block. Stacked on amd#5071. Merge that one first. This changes what the model sees, but only on the Claude provider. The default local-model flagship eval doesn't exercise it, so the eval that matters is a Claude-backed run. ## Test plan - [ ] `python -m pytest tests/unit/test_claude_provider.py`: covers thinking captured on create and stream with signatures, re-sent on the matching tool turn, redacted_thinking round-trip, interleaved order, unsigned blocks not replayed, and edited history, a changed tool set or a changed system prompt never sending stale blocks - [ ] `python util/lint.py --all` - [ ] Run a Claude-backed `gaia eval agent` and compare it with the same run on `main`: output tokens per call should drop, with no new failures - [ ] With a real `ANTHROPIC_API_KEY`, a two-step tool loop with thinking on is accepted, with no 400
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.
Every call a GAIA agent made to Claude re-billed the whole conversation as fresh input. Only the tool schemas and system prompt were cached, so each step of an agent loop paid full input price for its growing history, when it could have read that history from cache at a small fraction of the price. Now each request also caches the conversation, and the next call reads it back.
Test plan
python -m pytest tests/unit/test_claude_provider.py: covers the rolling breakpoint on create and stream, string and list content, a trailing tool_result, blocks that can't carry a marker, the caller's messages left unchanged, the 4-breakpoint limit, and a client wrapper that re-marks the last blockpython util/lint.py --allANTHROPIC_API_KEY:python -m pytest tests/integration/test_claude_history_cache.py. Call 2 must reportcache_read_input_tokenscovering call 1's history. Not run yet: it skips without a billed key, and no CI lane carries one.