Repository navigation
fix(llm): send thinking blocks back with their tool turn on the Claude provider - #5072
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.
…e provider With thinking enabled, a Claude tool loop re-derived its plan on every call. The provider dropped each turn's thinking blocks and signatures, and the agent's history keeps only text and tool calls, so the model never saw its own earlier reasoning. Now each tool turn goes back exactly as Claude produced it. - Each tool turn that carries thinking is kept inside the provider, with thinking and redacted_thinking blocks and their signatures unchanged. The stream path rebuilds the blocks from thinking_delta and signature_delta events. The agent's message format does not change. - On the next request a kept turn is matched on its tool_use ids and re-sent verbatim, interleaved order included. - Anthropic rejects a thinking block once the system prompt, tools or earlier messages change. The provider applies the same check: it digests everything sent before the turn (cache markers excluded) and re-sends the blocks only when that digest and the turn's text and tool calls are unchanged. Otherwise the turn goes back as it is now, without the stale blocks, and a debug log says why. One edit voids every later turn, as the API does. - Thinking blocks are left out of the digest, because the API chains each block to the one before it. Dropping the oldest turns' thinking first therefore stays valid, so a bounded store of 256 turns sheds its oldest without voiding the rest. - Unsigned blocks (a cut-off stream) and turns holding other block types are not kept. Thinking blocks never take the rolling cache breakpoint.
|
Holding this one. Replaying thinking for tool turns only, and not for the plain-answer turns between them, leaves a gap in the thinking chain. Claude Opus 5.5, Sonnet 5.5 and Fable 5.1 reject that gap with a 400, so the second question in any conversation would fail on those models, and today it works. It is also stacked on #5071, so it can't land on Next step: only replay the newest unbroken run of kept turns (sketch below), add a test for a conversation with two questions, then retarget to 🔍 Technical details
|
# Conflicts: # src/gaia/llm/providers/claude.py
…de provider Only tool turns' thinking was stored, so a second question went out with thinking on turns [T1], [], [T2]. Claude Opus 5.5, Sonnet 5.5, Fable 5.1 and Haiku 5.5 reject that gap with a 400, because kept thinking must be an unbroken run of the original sequence. Any assistant turn whose thinking can't be restored (a final answer, a cut-off stream, an edited turn) now ends the run, and every older turn goes back with only its thinking removed. Once a turn's thinking has been dropped it is never sent again.
|
Fixed the thinking gap. A second question in a conversation no longer sends thinking with a hole in the middle, so Opus 5.5, Sonnet 5.5 and Fable 5.1 won't 400 on it. The branch is merged up with 🔍 Technical details
|
|
Verdict: Approve with suggestions On the Claude provider, a tool turn now goes back to the API with the thinking Claude produced for it, so a tool loop with thinking on no longer works out its plan again on every call. Replay is careful: anything the API might reject is left out, never sent. That covers an edited turn, a changed system prompt, tool set or earlier message, a gap in the run of turns, and a stream cut off before its signature arrived. If a check misses, the cost is a lost optimisation, not a 400. Nothing blocks the merge. One small hardening is suggested below. The real test for this change is the Claude-backed eval and live-API items in the test plan. Mocked unit tests can't show that Anthropic accepts the replayed blocks, so tick those two items before merging. Real-world evidenceN/A: no CLI, API or MCP surface changed; this is internal to the Claude provider. The verdict rests on static review and the PR's unit tests, which pass in CI. Real Claude acceptance (no 400 on a two-step thinking loop) is still unverified. 🔍 Technical detailsIssues 🟢 The replay check doesn't include the model ( 🟢 Losing an earlier turn's stored blocks can unnecessarily drop a later turn's thinking. The digest folds in each restored turn as Claude produced it, including sanitized tool names and blocks in their original order. Suppose the store evicts T1, or T1 stops matching. T1 then goes into the digest as GAIA rebuilds it from history, with GAIA tool names and text before calls. Whenever those two forms differ (interleaved text, or MCP-style names with Strengths
|
Switching model partway through a Claude tool loop sent thinking signed by one model on a request to another. The replay digest now includes the resolved model, so such a turn goes back without its thinking. A non-Claude model id that resolves to the same Claude model is not a switch.
|
Both review suggestions are in: thinking is now tied to the model that produced it, so switching models mid-loop never sends one model's thinking to another. The branch is current with 🔍 Technical details
|
## Why this matters v0.25.2 is a bug-fix release on top of v0.25.1: setup and install no longer stop at the first failed step, shell and file access handle Windows paths and text encodings, the Agent UI and desktop app stop showing misleading states, and document search, email and skills each lose a handful of rough edges. 55 commits since v0.25.1; notes are in `docs/releases/v0.25.2.mdx` and credit 15 outside contributors. ## Test plan - [x] `python util/validate_release_notes.py docs/releases/v0.25.2.mdx --tag v0.25.2` passes - [x] `mintlify validate` (from `docs/`) passes - [x] `python util/check_doc_links.py --internal-only`, `python util/check_doc_versions.py` and `node installer/version/bump-ui-version.mjs --check` pass - [x] `python util/check_component_core_api.py --release-version 0.25.2` passes - [x] `pytest tests/unit/test_docs_json_release.py tests/unit/test_check_component_core_api.py tests/unit/test_release_tag.py tests/unit/test_hub_core_version_gate.py`: 156 passed - [ ] CI green on the merge commit - [ ] `release_train_dryrun.yml` passes on that commit before the RC tag ## Evals to re-run on the RC tag LLM-affecting PRs merged without per-PR evals; run these on `v0.25.2-rc1`: - `gaia_shell` and shell/safety (amd#5794, amd#5751, amd#5768) - `gaia_honesty` (amd#5753) - `gaia_skills_tasks` and `gaia_skills_lifecycle` (amd#5422, amd#5755) - `rag_retrieval` and `rag_quality` (amd#5742, amd#5754, amd#5761, amd#5817, amd#5812) - email scenarios (amd#5767, amd#5811) - `tool_selection` and `error_recovery` (amd#5732) - Claude provider thinking replay (amd#5072) - memory categories (amd#5818 merged; amd#5756 has not) ## Release checklist - [ ] Freeze main, wait for green CI on the exact commit, pass `release_train_dryrun.yml`, then push `v0.25.2-rc1` - [ ] Extend the changelog if main gains commits before the tag
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 #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 blockspython util/lint.py --allgaia eval agentand compare it with the same run onmain: output tokens per call should drop, with no new failuresANTHROPIC_API_KEY, a two-step tool loop with thinking on is accepted, with no 400