Skip to main content

Module subagents

Module subagents 

Source
Expand description

Claude Code subagent transcripts.

Claude Code (2.1.x) writes each subagent’s conversation to a file of its own, <dir of transcript_path>/<session id>/subagents/agent-<id>.jsonl, next to an agent-<id>.meta.json. The main transcript holds none of the subagent’s assistant lines or usage, so a hook that tails only transcript_path misses every token a subagent spends (VIS-1078).

Every line in those files carries the subagent’s agentId and the parent sessionId. The stream hook sends each file’s new lines under the parent session, one file per request, with transcript_offset counted within that file; the server keys the chunks on the line’s own agentId, so they never collide with the main transcript’s chunk indexes.

Functions§

offset_path
The file that holds how far transcript has been sent, keyed by its file stem (agent-<id>). Separate from the main transcript’s .stream_offset.
read_batch
read_new_lines, reading no further than the line that reaches max_bytes, and telling where each line ends (see crate::commands::stream::TranscriptBatch).
read_new_lines
Read the lines appended to transcript since the offset stored in offset_path (0 when absent). Returns (lines, start_offset, end_offset) with the meaning of commands::stream::read_new_transcript_lines: start_offset goes on the request as transcript_offset, end_offset is the next read position.
transcripts
The session’s subagent transcripts, sorted by path so a hook sends them in a stable order. Empty when the session has none (or transcript_path is empty, as with fileless agents). Only agent-<id>.jsonl is taken: the directory also holds agent-<id>.meta.json and forked-skill sidecars.
try_lock
Take the exclusive lock on the file offset_path keys, without waiting. None means another hook of the session holds it.
write_offset
Persist offset as the next read position of the file offset_path keys. Replaced atomically: a hook killed mid-write must leave the old offset, not an empty file every later read rejects as corrupt.