Answer: Read the session ID from the `session_id` field on the result message, which is present on every result regardless of success or error. In TypeScript the ID is also available earlier as a direct field on the init `SystemMessage`; in Python it's nested inside `SystemMessage.data`. Status: Confirmed Entry: tool/claude-code/agents-and-automation/capture-session-id Revision: 1 Digest: sha256:1b2750006c6f4d8f (changes whenever the content changes) Timeline: Created: 2026-11-13T03:02:57Z Last changed: 2026-11-13T03:02:57Z Last verified: 2026-11-20T12:17:16Z Next check: 2026-11-21T12:17:16Z Check interval: Daily Expected change: No fixed schedule Data: Field | Value | Source | As of session ID field | session_id | [1] | 2026-11-20 TypeScript result message type | SDKResultMessage | [1] | 2026-11-20 Python result message type | ResultMessage | [1] | 2026-11-20 TypeScript earlier availability | SystemMessage | [1] | 2026-11-20 Python nested location | SystemMessage.data | [1] | 2026-11-20 Sources: [1] Anthropic. "Work with sessions". https://code.claude.com/docs/en/agent-sdk/sessions.md Role: primary. Accessed: 2026-11-20T12:17:16Z Quote: "Read it from the `session_id` field on the result message" (Supports: session ID field) Quote: "[`SDKResultMessage`](https://code.claude.com/docs/en/agent-sdk/typescript#sdkresultmessage) in TypeScript" (Supports: TypeScript result message type) Quote: "[`ResultMessage`](https://code.claude.com/docs/en/agent-sdk/python#resultmessage) in Python" (Supports: Python result message type) Quote: "In TypeScript the ID is also available earlier as a direct field on the init `SystemMessage`" (Supports: TypeScript earlier availability) Quote: "in Python it's nested inside `SystemMessage.data`" (Supports: Python nested location) Published: 2026-11-20T02:52:44Z Snapshot SHA-256: 456f033eff0db40bef3d41724e8ee7260cd156256727d73859c4ee108c7e35d8 (captured 2026-11-13T03:02:57Z) Verification: Method: Automated extraction from [1] with deepseek-flash (JSON output, thinking disabled). Every value was checked by code to appear verbatim in a quoted passage of [1]. On this check the text of [1] was unchanged since 2026-11-13T03:02:57Z, so the values verified then were kept. Cross-check: not-available at 2026-11-20T12:17:16Z. No independent second source is configured for this entry Last successful check: 2026-11-20T12:17:16Z Last attempt: 2026-11-20T12:17:16Z Notes and limitations: Extracted automatically from a single official source (section "Capture the session ID"). Related: https://latestn.com/tool/claude-code/agents-and-automation/change-system-prompt-existing-session How do I change the system prompt of an existing Claude Code session? https://latestn.com/tool/claude-code/agents-and-automation/check-running-agent-work How do I check on work that Claude Code agents are running in the background? https://latestn.com/tool/claude-code/agents-and-automation/choose-and-weight-graders What grader types exist for Claude Code plugin evals and how do I choose and weight graders? https://latestn.com/tool/claude-code/agents-and-automation/available-hook-events What hook events are available in Claude Code? https://latestn.com/tool/claude-code/agents-and-automation/build-bug-fixing-agent How do I build and run an agent with the Claude Code Agent SDK that finds and fixes bugs? https://latestn.com/tool/claude-code/agents-and-automation/call-model-from-mod How do I call a model from a Claude Code mod with the mods API? https://latestn.com/tool/ All entries in AI tools Change log: Time | Event | Change | Source 2026-11-13T03:02:57Z | added | Entry created | — Changes for this entry as a feed: https://latestn.com/tool/claude-code/agents-and-automation/capture-session-id/latest Challenge this entry: Open challenges: None How: If a value here is wrong or out of date, send what is wrong and a link to evidence. Challenges are queued and checked against the evidence; they do not change the entry by themselves. If the entry changes as a result, the change log says so. Form: https://latestn.com/dispute/tool/claude-code/agents-and-automation/capture-session-id POST: https://latestn.com/dispute/tool/claude-code/agents-and-automation/capture-session-id with claim, evidence_url, and optionally field and contact (form-encoded or JSON) GET: https://latestn.com/dispute/tool/claude-code/agents-and-automation/capture-session-id?claim={what is wrong}&evidence_url={link} How to cite: LatestN. (2026). How do I capture the session ID in Claude Code SDK? Revision 1, last verified 2026-11-20T12:17:16Z. https://latestn.com/tool/claude-code/agents-and-automation/capture-session-id (accessed 2026-11-21T09:58:56Z). @misc{LatestN_tool_claude_code_agents_and_automation_capture_session_id_r1, title = {How do I capture the session ID in Claude Code SDK?}, author = {{LatestN}}, year = {2026}, howpublished = {\url{https://latestn.com/tool/claude-code/agents-and-automation/capture-session-id}}, note = {Revision 1; digest sha256:1b2750006c6f4d8f; last verified 2026-11-20T12:17:16Z; accessed 2026-11-21T09:58:56Z} } Reuse: License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/) Attribution: Copying and adapting are allowed, including commercially, if you credit LatestN and link to the entry you used. Each entry ends with a ready-made citation. URL: https://latestn.com/tool/claude-code/agents-and-automation/capture-session-id