Manages agent session lifecycle across Claude Agent SDK (local transcripts with continue, resume, forkSession) and Managed Agents (server-side sessions with idle, running, rescheduling, terminated states). Covers session ID capture, cross-process resume, forking alternatives, and retention policies.
Ships in agent-sdk. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
"Session" means different things in the two Anthropic agent products. In the Agent SDK it is a conversation transcript on local disk that you continue, resume, or fork. In Managed Agents it is a server-side resource with a status that you drive with events. Do not mix their APIs.
| Agent SDK | Managed Agents | |
|---|---|---|
| Where state lives | Transcript files on the machine that ran the agent | Anthropic servers |
| How you continue | continue, resume, forkSession options on query() | Send more events to the same session |
| Fork | Yes (forkSession: true / fork_session=True) | No |
| Cleanup | Your files or session-store adapter | sessions.archive / sessions.delete |
The SDK docs give this guidance:
| You are building | Use |
|---|---|
| One-shot task | One query() call; nothing extra |
| Multi-turn chat in one process | ClaudeSDKClient (Python) or continue: true (TypeScript) |
| Pick up after a restart, one conversation at a time | continue: true / continue_conversation=True (most recent session in the directory) |
| Return to a specific session | Capture its ID and pass resume |
| Try an alternative without losing the original | resume plus forkSession: true |
| Nothing written to disk | persistSession: false (TypeScript only) |
Read session_id from the result message (present on success and error results). In TypeScript it is also on the system init message.
import { query } from "@anthropic-ai/claude-agent-sdk";
let sessionId: string | undefined;
try {
for await (const message of query({ prompt: "Analyze the auth module", options: { allowedTools: ["Read", "Glob", "Grep"] } })) {
if (message.type === "result") sessionId = message.session_id;
}
} catch (error) {
// query() throws after yielding an error result; sessionId was already captured
}
// Continue the same history
for await (const m of query({ prompt: "Now implement the refactor you suggested",
options: { resume: sessionId, allowedTools: ["Read", "Edit", "Glob", "Grep"] } })) { /* ... */ }
// Branch: new session ID, original history unchanged
for await (const m of query({ prompt: "Instead, outline an OAuth2 approach",
options: { resume: sessionId, forkSession: true, maxTurns: 5 } })) {
if (m.type === "system" && m.subtype === "init") console.log("fork:", m.session_id);
}
from claude_agent_sdk import query, ClaudeAgentOptions
async for m in query(prompt="Continue with the JWT approach",
options=ClaudeAgentOptions(resume=session_id)):
...
async for m in query(prompt="Try OAuth2 instead",
options=ClaudeAgentOptions(resume=session_id, fork_session=True)):
...
Sessions and forks branch the conversation, not the filesystem. File edits made in a fork are real; use the SDK's file checkpointing to revert them.
Transcripts are local files (under ~/.claude/projects/<encoded-cwd>/ by default, or under $CLAUDE_CONFIG_DIR/projects/). To resume on a different machine, either attach a session-store adapter (sessionStore / session_store) so transcripts mirror to your backend, copy the transcript file to the new host, or skip resume and pass the results you need into a fresh session's prompt. The last option is often the most robust. The SDKs also expose listSessions() / list_sessions() and getSessionMessages() / get_session_messages() for building pickers and cleanup jobs.
rescheduling -> running <-> idle -> terminated
idle: waiting for input, blocked on a tool confirmation or custom tool result (stop_reason requires_action), or paused at its budget (budget_reached)running: the agent is working; new events are queuedrescheduling: recovering from a retryable errorterminated: over, either completed or failed; retrieve the session to tell whichContinue a session by sending another user.message event to the same session ID (see managed-agents-api). Compaction and prompt caching are built into the harness. To change tools for one running session, sessions.update accepts session-local overrides while the session is idle.
Start a new session when:
Carry forward only what matters (decisions, file paths, open questions) in the new prompt or in a memory store.
Retention is a policy decision, not a technical default to copy. Agent SDK transcripts stay on disk until you delete them; Managed Agents sessions persist until you call sessions.delete (archive keeps them read-only). Decide per use case how long you keep sessions, document it, and automate deletion.
terminated as "inspect the session", not "it failed"What SKILL.md sets; muted values are Claude Code's defaults
/agent-sdk:session-lifecyclefrom agent-sdk
npx claudepluginhub latestaiagents/agent-skills --plugin agent-sdk5 skills in this plugin
/agent-sdk-quickstartBuild a first agent with the Claude Agent SDK (TypeScript or Python): install, run query(), read result messages, restrict tools and permissions, add custom tools through an in-process MCP server, and guard tool calls with hooks. Also explains when to pick the Agent SDK, the Client SDK, or Managed Agents.
Use this skill when starting a new agent project, migrating from a hand-written messages.create tool loop, or choosing between the Agent SDK, the Client SDK, and Managed Agents.
Activate when: Claude Agent SDK, @anthropic-ai/claude-agent-sdk, claude-agent-sdk, query(), ClaudeAgentOptions, createSdkMcpServer, build agent, agent SDK quickstart.
/agent-sdk-streamingStream Claude Agent SDK output to a UI: the default message flow, enabling partial messages with includePartialMessages / include_partial_messages, handling stream_event deltas for text and tool input, attributing subagent output, and handling cut-off streams.
Use this skill when building a chat or progress UI on top of the Agent SDK, showing tool activity live, or debugging missing or out-of-order streamed output.
Activate when: agent SDK streaming, includePartialMessages, include_partial_messages, stream_event, StreamEvent, SDKPartialAssistantMessage, text_delta, input_json_delta, real-time agent output.
Other plugins' skills for the same job
2 plugins in one repository ship this exact skill · first indexed Nov 5, 2026