Skip to content

Context and model loop

This chapter follows a Claude Code turn from input collection to provider-facing request and streamed response handling. It covers what becomes model-visible context, how model/provider/auth state is selected, and how headless/SDK streaming frames drive a scriptable run.

Read this chapter when the question is: what did the model see, which provider/model handled it, and how did the runtime frame the turn?

Source-anchor policy

This page is a chapter guide. Linked implementation pages carry concrete cli.renamed.js anchors.

Semantic aliasMinified anchorScope
Context/model chapterN/A — navigation pageGroups prompt/context assembly, memory, settings, provider/auth selection, and headless streaming.
Context/model implementation pagesSee linked source-anchor tablesConcrete bundle anchors live in destination pages.

Model-turn map

flowchart TD
Input[Prompt / stdin / files / settings] --> Context[Prompt and context sources]
Context --> Memory[CLAUDE.md / rules / output styles / slash commands]
Context --> Tools[Tool schemas and agent definitions]
Memory --> Request[Provider request]
Tools --> Request
Auth[Auth and provider selection] --> Request
Request --> Stream[Streaming response]
Stream --> Headless[Headless stream-JSON frames]
Stream --> TUI[Interactive session loop]
Headless --> Events[Session events and result]
TUI --> Events

Primary reading order

OrderPageQuestion answered
1Prompt, context, and memoryWhich CLAUDE.md, settings, system-prompt, output-style, slash-command, memory, system-reminder, and dynamic-injection sources feed context?
2Prompt assembly scenariosHow do major runtime paths assemble default, custom, appended, bare, agent, subagent, WebSearch/WebFetch, compaction, hook, and helper prompts?
3Context, memory, compaction, checkpoints, and rewindHow are memory scopes selected; what do /context, /compact, and /autocompact measure or mutate; and how do file checkpoints plus /rewind (/checkpoint, /undo) work?
4Prompt template catalogWhich long prompt/template-like literals are embedded in cli.renamed.js, grouped by runtime surface with anchors, previews, and hashes?
5Team memoryHow do explicit/discovered multi-store descriptors, local mirrors, synchronization, path safety, and prompt-index exposure compose?
6Models, providers, and authHow do API keys, OAuth tokens, provider env vars, model flags, and provider selection connect?
7Model selection, calls, usage, quota, and billingHow are main-turn, resume, Agent, Workflow, skill, helper, and fallback models resolved, and how do /model, /effort, /fast, /usage, /usage-credits, limits, budgets, and billing work?
8Headless streaming and resilienceHow does -p/SDK mode validate formats, multiplex frames, prioritize queued prompts, and distinguish interrupt, async-message cancellation, and correlated control cancellation?
9Context and model loop architectureHow is the context assembler + streaming multiplexer decomposed, what shapes the static/dynamic prompt boundary, and how do interactive and headless reuse one pipeline?

Handoffs

Created and maintained by Yingting Huang.