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 alias | Minified anchor | Scope |
|---|---|---|
| Context/model chapter | N/A — navigation page | Groups prompt/context assembly, memory, settings, provider/auth selection, and headless streaming. |
| Context/model implementation pages | See linked source-anchor tables | Concrete 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 --> EventsPrimary reading order
| Order | Page | Question answered |
|---|---|---|
| 1 | Prompt, context, and memory | Which CLAUDE.md, settings, system-prompt, output-style, slash-command, memory, system-reminder, and dynamic-injection sources feed context? |
| 2 | Prompt assembly scenarios | How do major runtime paths assemble default, custom, appended, bare, agent, subagent, WebSearch/WebFetch, compaction, hook, and helper prompts? |
| 3 | Context, memory, compaction, checkpoints, and rewind | How are memory scopes selected; what do /context, /compact, and /autocompact measure or mutate; and how do file checkpoints plus /rewind (/checkpoint, /undo) work? |
| 4 | Prompt template catalog | Which long prompt/template-like literals are embedded in cli.renamed.js, grouped by runtime surface with anchors, previews, and hashes? |
| 5 | Team memory | How do explicit/discovered multi-store descriptors, local mirrors, synchronization, path safety, and prompt-index exposure compose? |
| 6 | Models, providers, and auth | How do API keys, OAuth tokens, provider env vars, model flags, and provider selection connect? |
| 7 | Model selection, calls, usage, quota, and billing | How 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? |
| 8 | Headless streaming and resilience | How does -p/SDK mode validate formats, multiplex frames, prioritize queued prompts, and distinguish interrupt, async-message cancellation, and correlated control cancellation? |
| 9 | Context and model loop architecture | How is the context assembler + streaming multiplexer decomposed, what shapes the static/dynamic prompt boundary, and how do interactive and headless reuse one pipeline? |
Handoffs
- Tool schemas and permissions are owned by Tools, integrations, and security.
- Durable transcript state is owned by Sessions, persistence, and remote.
- Agents and task-specific prompts are owned by Agents and automation.
Navigation
Created and maintained by Yingting Huang.