[BUG] Agent SDK session resume loses all tool_use/tool_result history - conversations marked as sidechain
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
When using the Agent SDK (@anthropic-ai/claude-agent-sdk) with session resume (passing a sessionId to query()), all tool_use/tool_result message pairs are missing from the restored context. The model retains conversational text (assistant responses) but has zero memory of any tool calls it made - edits, bash commands, file reads, MCP tool invocations, everything.
This does not affect the CLI. Running claude --resume <id> correctly restores tool call history. The bug is specific to the Agent SDK's session serialization path.
Root Cause (from source analysis)
The CLI's prettified source reveals two codepaths for insertMessageChain:
- CLI path calls with
isSidechain = false- messages are written to the main session JSONL (<sessionId>.jsonl) - Agent SDK path calls with
isSidechain = true- messages are routed toagent-<id>.jsonlfiles instead
On session resume, only the main session JSONL is loaded. The agent sidechain files are not re-ingested, so all tool call history is lost. The main session file contains only queue operations, the initial user prompt, assistant text blocks, and a last-prompt marker - typically 6 entries for an entire session regardless of how many tool calls were made.
Evidence
Test 1 - Agent SDK (bot using query() with session resume):
- Session made 4 tool calls: Write (create file), Edit (append line with magic number 42), Bash (echo timestamp), Read (confirm edits)
- Model narrated each step in assistant text
- Session was restarted via SoftRestart (preserves sessionId)
- Post-restart: model insisted it "never ran the test" despite file existing on disk with correct content
- SDK session JSONL contained 6 entries, zero
tool_useblocks
Test 2 - CLI (claude --resume):
- Identical test protocol
- After resume, all four
tool_useandtool_resultpairs were present in context - Model could recall details only present in tool_result blocks (line numbers, path formatting)
Test 3 - Timing control:
A session made tool calls at ~02:05 UTC, then continued conversing for over an hour until ~03:10 UTC. After restart, the tool calls were still missing. This rules out any flush-timing explanation - the SDK had 65 minutes of active operation after the tool calls and still never persisted them.
JSONL comparison for the same session:
Our own context logger (streaming writes to a separate JSONL) captured everything:
Message types: assistant: 24, user: 14, system: 4, result: 4
Tool calls: Bash: 1, Edit: 1, Read: 5, Write: 1, ToolSearch: 1, SoftRestart: 1
The SDK's session JSONL for the same session:
Message types: assistant: 2, user: 1, queue-operation: 2, last-prompt: 1
Tool calls: (none)
Steps to Reproduce
- Create an Agent SDK application that uses
query()with a persistentsessionId - In the session, make several tool calls (Read, Edit, Bash - anything)
- End the session (let the query complete)
- Start a new
query()with the samesessionId(session resume) - Ask the model what tool calls it made in the previous session
- Model will have no memory of tool calls, only of conversational text
What Should Happen?
Session resume through the Agent SDK should restore the full conversation history including tool_use/tool_result pairs, matching the behavior of claude --resume in the CLI. The agent SDK's insertMessageChain should use isSidechain = false for the primary conversation, or the resume logic should re-ingest agent sidechain files.
Environment
@anthropic-ai/claude-agent-sdk: 0.2.81 (also confirmed on 0.2.83)- Claude Code CLI: 2.1.85
- Node.js: v24.12.0
- Platform: Linux (Debian 14, x86_64)
- Model: claude-opus-4-6 (1M context)
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗