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.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, how does auto/manual compaction summarize context, and how do file checkpoints plus rewind work?
4Prompt template catalogWhich long prompt/template-like literals are embedded in cli.js, grouped by runtime surface with anchors, previews, and hashes?
5Models, providers, and authHow do API keys, OAuth tokens, provider env vars, model flags, and provider selection connect?
6Model selection, calls, usage, quota, and billingHow are model roles resolved, how are provider calls made, and how do rate limits, usage, quota, budget, and billing surfaces work?
7Headless streaming and resilienceHow does -p/SDK mode validate input/output formats, stream messages, multiplex frames, and handle permission/control/result framing inside HeadlessRunner/HeadlessControlLoop?
8Context 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.