[BUG] Agent SDK session resume loses all tool_use/tool_result history - conversations marked as sidechain

Resolved 💬 2 comments Opened Mar 27, 2026 by nicolasnoble Closed Apr 26, 2026

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 to agent-<id>.jsonl files 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):

  1. Session made 4 tool calls: Write (create file), Edit (append line with magic number 42), Bash (echo timestamp), Read (confirm edits)
  2. Model narrated each step in assistant text
  3. Session was restarted via SoftRestart (preserves sessionId)
  4. Post-restart: model insisted it "never ran the test" despite file existing on disk with correct content
  5. SDK session JSONL contained 6 entries, zero tool_use blocks

Test 2 - CLI (claude --resume):

  1. Identical test protocol
  2. After resume, all four tool_use and tool_result pairs were present in context
  3. 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

  1. Create an Agent SDK application that uses query() with a persistent sessionId
  2. In the session, make several tool calls (Read, Edit, Bash - anything)
  3. End the session (let the query complete)
  4. Start a new query() with the same sessionId (session resume)
  5. Ask the model what tool calls it made in the previous session
  6. 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)

View original on GitHub ↗

This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗