Skip to main content

Prompt Suggestions & Speculation

The main REPL loop has a sidecar system for suggesting the user's next prompt and, when enabled, speculating ahead on that candidate prompt before the user accepts it.

Where It Sitsโ€‹

โŒจ๏ธ
Main REPL turn completes
๐Ÿ’ก
Prompt suggestion service
Store suggestion for the UI
If speculation is enabled
๐Ÿ”ฎ
Start speculative fork
Run ahead on the suggested next turn

This is adjacent to the agent harness, not part of the core loop itself. The main turn finishes normally first. Suggestion generation and speculation are follow-on services driven by src/services/PromptSuggestion/.

Prompt Suggestionsโ€‹

src/services/PromptSuggestion/promptSuggestion.ts decides whether a suggestion should exist at all:

GateSource-Backed Behavior
Global enablementCLAUDE_CODE_ENABLE_PROMPT_SUGGESTION can override the default gate
Feature flagGrowthBook flag tengu_chomp_inflection gates the feature
Interactive modeDisabled in non-interactive runs
Swarm behaviorDisabled for swarm teammates; only the leader shows suggestions
User settingRespects promptSuggestionEnabled !== false

Suggestion generation is also suppressed when the REPL is in a state where a follow-up prompt would be misleading:

Suppress ReasonWhy It Exists
Pending permission requestThe current turn is still waiting on approval
Elicitation queue activeAnother structured prompt/elicitation is already in progress
Plan modeThe UI is in a different interaction mode
External user with blocked limitsCurrent product limits do not allow suggestions

The service only attempts generation after at least two assistant turns, and it skips generation if the last assistant message was an API error. When a suggestion is accepted, it becomes the next real user message.

Speculation Is a Forked Overlay Runโ€‹

If prompt suggestions are enabled and speculation is allowed, Claude Code can fork a speculative run on top of the suggested next prompt:

๐Ÿ’ก
Suggested next prompt
๐Ÿงช
Create speculation session + overlay directory
Speculative Run
๐Ÿ“–
Allow read-only tools to continue
โœ๏ธ
Redirect writes into overlay when allowed
๐Ÿ›‘
Stop at a boundary if speculation becomes unsafe
๐Ÿ“ฆ
Wait for user to accept or ignore

The overlay is not a git worktree. src/services/PromptSuggestion/speculation.ts creates a copy-on-write directory under Claude's temp directory and redirects file reads and writes through it during the speculative run.

Tool Boundariesโ€‹

The speculation engine is intentionally narrow:

Tool BehaviorSource-Backed Rule
Safe read-only tools continueRead, Glob, Grep, ToolSearch, LSP, TaskGet, TaskList
Write tools may continueEdit, Write, NotebookEdit, but only if permissions effectively allow auto-accept edits
Bash boundaryNon-read-only shell commands stop speculation
Default boundaryAny other unapproved tool stops speculation

The source also imposes hard limits of MAX_SPECULATION_TURNS = 20 and MAX_SPECULATION_MESSAGES = 100.

Accepting a Speculative Resultโ€‹

When the user accepts a suggestion, the speculative state can be promoted back into the main session:

StepSource-Backed Behavior
Abort speculationStop the speculative run and clean up timers/state
Copy overlay backcopyOverlayToMain(...) copies overlay files into the working directory
Merge messagesSpeculated messages are cleaned and injected into the main transcript
Merge read cacheExtracted read-file cache entries are promoted back into the main session
Promote next suggestionIf speculation completed cleanly, a pipelined next suggestion may also be surfaced

This is what makes speculation feel fast: some read, edit, and reasoning work may already be complete before the user actually submits the next turn.

Key Source Filesโ€‹

FilePurpose
src/services/PromptSuggestion/promptSuggestion.tsSuggestion gating, suppression rules, and suggestion generation
src/services/PromptSuggestion/speculation.tsSpeculative fork execution, overlay filesystem, and accept/merge flow