Why Sessions Matter
By default, each query() call starts fresh — Claude has no memory of previous interactions. That works for one-off tasks, but real-world agents need continuity. A coding assistant should remember what file it was working on. A research agent should build on previous findings. A customer service bot should know the conversation history.
Sessions are how you give agents memory. A session stores the conversation history so Claude can pick up where it left off — even after your application restarts.
Multi-Turn Conversations
The simplest form of session management is multi-turn conversations within a single run. The SDK handles this automatically when you make multiple queries on the same agent instance:
import { Claude } from "@anthropic-ai/claude-agent";
const agent = new Claude({
model: "claude-sonnet-4-6",
tools: "defaults",
});
// First query — Claude reads the file
const r1 = await agent.query("Read src/index.ts and explain what it does.");
console.log(r1.text);
// Second query — Claude remembers the first conversation
const r2 = await agent.query(
"Now add error handling to the main function you just read.",
{ sessionId: r1.sessionId } // resume the same session
);
console.log(r2.text);
// Third query — still in the same conversation
const r3 = await agent.query(
"Write tests for the changes you just made.",
{ sessionId: r2.sessionId }
);
console.log(r3.text);