Subagents, Sessions, Resume and Checkpoints
Subagents are separate agent instances that Claude spawns through the `Agent` tool to keep context clean, run work in parallel and apply narrower tools or models. Sessions are the JSONL conversation history Claude Code writes to disk, which you can continue, resume, fork or mirror to your own store. Know what a subagent inherits and returns, how background, fork and nested subagents behave, how to pick up a session reliably across restarts and hosts, and what file checkpointing can and cannot undo.
Key points
- 1
Define subagents with the
agentsoption: a map of name toAgentDefinition. Onlydescriptionandpromptare required. Optional fields includetools,disallowedTools,model(an alias, a full ID orinherit),skills,mcpServers,maxTurns,background,effort,permissionModeandmemory. TypeScript addsomitClaudeMdandautoCompactWindow. Programmatic agents override.claude/agents/files with the same name, and the built-ingeneral-purposeagent is always available unlessCLAUDE_AGENT_SDK_DISABLE_BUILTIN_AGENTS=1is set. - 2
Claude delegates through the
Agenttool (calledTaskbefore Claude Code v2.1.63, and stillTaskin thesystem:inittools list), so match both names when detecting launches. Messages from inside a subagent carryparent_tool_use_id; TypeScript messages also carryagent_id. - 3
A non-fork subagent starts fresh. It gets its own prompt, the Agent tool's prompt string, CLAUDE.md (unless
omitClaudeMd), tool definitions and the parent's thinking setting. It does not get the parent's history, system prompt or loaded skills. Only its final message returns to the parent, which may paraphrase it. - 4
Tool scoping: if
toolsis omitted, the subagent gets every tool available to subagents. A listed set means only those tools, and a missing tool simply is not there (no prompt, no error).disallowedToolsis applied first and removes whole tools even when an entry has a specifier.AskUserQuestion,EnterPlanMode,Workflowand a few others are removed from every subagent, and background subagents keep only a reduced built-in set. - 5
Subagents start in the background by default: the Agent result is
status: "async_launched"and the report arrives in a later turn. Claude waits (status: "completed") when its next step needs the report or when the prompt asks it to.background: truekeeps an agent in the background. - 6
Nesting is on by default, up to 3 layers (
CLAUDE_CODE_MAX_SUBAGENT_SPAWN_DEPTH, where1means no nesting). At most 20 subagents run at once (CLAUDE_CODE_MAX_CONCURRENT_SUBAGENTS).maxBudgetUsdcounts subagent spend; at the cap it refuses spawns, stops background subagents and ends witherror_max_budget_usd. Pass the env variables throughenv; TypeScript'senvreplaces the environment, so spreadprocess.env. - 7
Forks (
subagent_type: "fork", which Claude can start with nothing configured since Claude Code v2.1.302) copy the whole conversation, system prompt, tools and model, and share the parent's prompt cache. Deny them alone withdisallowedTools: ["Agent(fork)"]. - 8
A subagent's
permissionModeis ignored under abypassPermissions,acceptEditsor auto parent, which the subagent inherits. Under adefault,dontAskorplanparent it applies, except thatbypassPermissionsis never granted to a subagent of a non-bypass parent. - 9
Model order for a subagent: the per-call
modelparameter, then the definition'smodel, thenCLAUDE_CODE_SUBAGENT_MODEL, then the main model (Claude Code v2.1.251 and later). Resume a subagent by resuming the parent session and naming theagentIdfrom the Agent result. Explore and Plan are one-shot and return noagentId. - 10
Sessions are stored at
~/.claude/projects/<encoded-cwd>/<session-id>.jsonl, where every non-alphanumeric character of the cwd becomes-. Read the ID from the result message (always present) or the init message.continuepicks the newest session in the directory;resumetakes an ID;forkSessionwithresumemakes a new ID and leaves the original alone. A customsessionIdUUID cannot be combined withresumeorcontinueunlessforkSessionis also set.persistSession: falseis TypeScript only; Python setsCLAUDE_CODE_SKIP_PROMPT_HISTORYinenv. - 11
resumeSessionAttruncates a resume at any chain-entry UUID.resumeDropsTurn(the prompt UUID of the turn being dropped) makes the CLI refuse when the discarded range holds anything else. The refusal is deterministic, so resume plainly instead of retrying. With structured output, fork at the kept turn's last entry (thestructured_outputattachment), not at the last assistant UUID. - 12
A
SessionStore(appendandloadrequired;listSessions,listSessionSummaries,deleteandlistSubkeysoptional) mirrors local writes. Keys includeprojectKey, derived from the cwd, so resume from a matching cwd. It cannot be combined withpersistSession: falseor with file checkpointing. File checkpointing (enableFileCheckpointingplus thereplay-user-messagesextra arg) restores only Write, Edit and NotebookEdit changes. To rewind after the stream ends, resume with an empty prompt and checkpointing on, then callrewindFilesinside the loop.
Common traps
Resuming or forking a session restores the conversation only. Files stay as they are on disk, and two forks in one directory overwrite each other's edits; use git worktrees.
continue: truein a multi-user service resumes whichever session in the directory is newest, which is often someone else's. Store IDs and useresume.Rewind does not undo Bash edits, directory operations, remote changes or most subagent edits, and it never rewinds the conversation.
Older posts say subagents can never spawn subagents. Current releases allow 3 layers by default; to stop one subagent, leave
Agentout of itstools.A subagent cannot ask the user anything:
AskUserQuestionis stripped even if listed. Have it report open questions instead of guessing.disallowedTools: ["Bash(git push *)"]in a subagent removes all of Bash. Block single commands with apermissions.denyrule.A
SessionStoremiss after moving to a new host is usually a differentcwdchangingprojectKey, not a storage bug.Retrying a refused
resumeDropsTurnresume fails forever; it is a deterministic refusal, not a transient error.
Read the source
Further reading
Suggest a resourceNo extra reading for Subagents, Sessions, Resume and Checkpoints yet. Found a guide, video or cheat sheet that made it click? Share it and help the next learner.
Test yourself on Subagents, Sessions, Resume and Checkpoints
Ten questions, with the answer and explanation after each one.