📚Academy
likeone
online

Session Management

Build agents that remember, resume, and fork conversations across restarts

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.

Real-world analogy: Sessions are like saving your game. Without them, you start from level 1 every time you open the app. With sessions, you load your save file and continue from where you stopped.

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:

TypeScript — multi-turn conversation
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);
🔒

This lesson is for Pro members

Unlock all 355+ lessons across 36 courses with Academy Pro. Founding members get 90% off — forever.

Already a member? Sign in to access your lessons.

Academy
Built with soul — likeone.ai