Skip to content

fix(llm): cache the conversation history on the Claude provider - #5071

Merged
kovtcharov-amd merged 1 commit into
mainfrom
fix/claude-history-cache
Oct 13, 2026
Merged

kovtcharov-amd merged 1 commit into
mainfrom
fix/claude-history-cache

Conversation

@kovtcharov-amd

Copy link
Copy Markdown
Collaborator

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 block
  • python util/lint.py --all
  • With a real ANTHROPIC_API_KEY: python -m pytest tests/integration/test_claude_history_cache.py. Call 2 must report cache_read_input_tokens covering call 1's history. Not run yet: it skips without a billed key, and no CI lane carries one.

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.
@github-actions github-actions Bot added llm LLM backend changes tests Test changes performance Performance-critical changes labels Oct 13, 2026
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 13, 2026
Merged via the queue into main with commit 158dd43 Oct 13, 2026
@kovtcharov-amd
kovtcharov-amd deleted the fix/claude-history-cache branch October 13, 2026 23:44
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm LLM backend changes performance Performance-critical changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant