An opinionated Agent base class for AI assistants. Think owns the chat lifecycle — the agentic loop, message persistence, streaming, client tools, resumable streams, durable recovery, and extensions — on Durable Object SQLite.
Status: experimental (@cloudflare/think).
This file records how Think is put together and why. For API usage see docs/think/index.md and the pages it links; for recovery behavior shared with AIChatAgent see docs/agents/chat-agents.md#stream-recovery.
Every chat agent needs the same infrastructure: durable message storage, streaming with cancellation, a tool loop with step limits, client-side tools and approvals, stream resumption after reconnect, and recovery when the Durable Object is evicted mid-turn. Agent provides the Durable Object primitives but no opinion on how to run a chat. AIChatAgent provides the protocol plumbing but leaves the model call to the subclass.
Think is the opinion: the subclass declares a model, tools, and context, and Think runs the turn.
Agent (agents) Lifecycle, Tasks, Scheduler, MCP, WebSockets, state
└─ Think<Env, State, Props> chat lifecycle, turn pipeline, tools, extensions
└─ YourAgent getModel(), getTools(), configureContext(), hooks
Think extends Agent and installs two Lifecycle capabilities in its constructor:
- Sessions (
agents/sessions) stores settled messages as a tree, with compaction overlays and on-demand FTS5 search. See sessions.md. - Streams (
createChatStreams()fromagents/chat) stores in-flight output.ResumableStreamfromagents/chatis layered over it for chunk replay.
It also registers Task definitions on the inherited this.tasks capability: __cf_internal_chat_turn for chat turns and their recovery attempts, __cf_internal_chat_recovery to finish the recovery runs an earlier version enqueued, and one for messenger replies. Prompt context blocks come from agents/context. The streaming, reconciliation, protocol, and recovery primitives are the shared layer in agents/chat — see chat-shared-layer.md.
Think wraps onStart in its constructor, so subclasses override onStart() without calling super.onStart(). Startup initializes the workspace (unless the subclass assigned one), configures the Session through configureSession(), loads context blocks from configureContext(), restores extensions, sets up the chat protocol handlers and channels, runs the subclass onStart, then reconciles declarative scheduled tasks.
Every way to start a turn converges on one private inference loop:
| Path | Entry |
|---|---|
| Browser WebSocket | cf_agent_use_chat_request frames from useAgentChat (@cloudflare/think/react) |
| Parent agent RPC | chat(message, callback, options) streaming through a StreamCallback |
| Programmatic | runTurn(), saveMessages(), submitMessages(), continueLastTurn(); addMessages() without a turn |
| Tool results and approval | auto-continuation after cf_agent_tool_result / cf_agent_tool_approval |
| Messengers | Chat SDK webhooks declared by getMessengers() (@cloudflare/think/messengers) |
| Scheduled tasks | getScheduledTasks() prompt tasks (durable submissions) or deterministic handlers |
| Workflows | ThinkWorkflow step.prompt() (@cloudflare/think/workflows) |
| Recovery | chat-recovery Task continuations after an interrupted turn |
StreamCallback is onStart, onEvent, onDone, onError, plus optional onInterrupted for a turn handed to bounded recovery. messageConcurrency ("queue" by default; also "latest", "merge", "drop", and debounce) decides how overlapping submits behave. TurnQueue from agents/chat serializes turns and invalidates queued work when the chat is cleared.
For each model run Think:
- Optionally waits for MCP connections (
waitForMcpConnections). - Merges tools, later sources overriding earlier ones on name collisions: workspace tools (
createAIToolsfrom@cloudflare/computer, with no options),fetchTools,getTools(), compiledgetActions(), extension tools, context tools, skill tools, MCP tools (whenincludeMcpTools), and client tool schemas. - Applies the active channel's tool filter and instructions from
configureChannels(). - Builds the system prompt from an overridden
getSystemPrompt()followed by the frozen context-block prompt. Think's defaultgetSystemPrompt()is used only when no context block renders. - Assembles model messages from the Session path, with compaction overlays applied.
- Calls
beforeTurn(ctx), then extensionbeforeTurnhooks. The returnedTurnConfigcan replace the model, system prompt, or messages and add tools. - Runs
streamTextwithstopWhen: stepCountIs(maxSteps)(default 10), threadingbeforeStep,beforeToolCall,afterToolCall,onStepFinish, andonChunk.
After the turn, onChatResponse(result) fires from every path; onChatError(error, ctx) customizes failures, and classifyChatError tells recovery which errors are context-window overflows (contextOverflow). There is no onChatMessage override: a subclass customizes pieces through hooks rather than replacing the loop.
getModel() throws until overridden. It may return a LanguageModel or a string; resolveModel() turns a string into a model through the bundled workers-ai-provider over getAIBinding() (default env.AI), routing "<provider>/<model>" slugs through AI Gateway with options from getGateway().
The streamText result is iterated as a UI message stream. Each chunk is applied to a StreamAccumulator, stored in the resumable stream, and broadcast to connected clients except those waiting to resume. The RPC path uses its own accumulator and forwards chunks to the StreamCallback.
When a turn finishes, Think persists the assistant message and settles and deletes its resumable stream in one SQLite transaction (the cutover), so a crash leaves either the stream (recovery rebuilds the message from it) or the message, never both. The transcript broadcast (cf_agent_chat_messages) is sent before the terminal done frame, because clients switch to ready on done and a later snapshot would overwrite a message the user sent in between (#2119).
Writes go through the Sessions pipeline, which sanitizes provider metadata and strips reserved metadata keys. Sessions never truncates: media parts move to content-addressed attachment rows, and a message too large for one row is split across continuation rows. Incoming client messages are reconciled against the server path with reconcileMessages before they are written.
mediaEviction (on by default) is Think's own context-window policy above storage. Aged media is replaced in the conversation with an [evicted <mediaType>, <bytes> bytes; preserved at <path>] marker and the bytes are written to the Workspace under /attachments/evicted/. hydrationByteBudget (32 MiB) bounds the startup read that fills the in-isolate messages projection; the Sessions change feed keeps that projection coherent afterwards.
Regeneration (trigger: "regenerate-message") appends a new assistant message under the same user parent, so earlier responses remain as branches readable with session.getBranches(). continueLastTurn() creates a new assistant message, except recovery continuations, which stream into the interrupted message.
Durable chat recovery is always on. Every chat turn runs as a __cf_internal_chat_turn Task, and chatRecovery accepts true (the default) or a tuning object, not false. When the object restarts and finds an interrupted turn, the shared ChatRecoveryEngine from agents/chat classifies it, persists any settled partial, calls onChatRecovery(ctx), and dispatches a retry or continuation under an incident budget, in the interrupted turn's own __cf_internal_chat_turn run or in one started for the attempt (see chat-turn-core.md). Clients see cf_agent_chat_recovering while this happens. Facet-hosted Think instances keep a root-owned Scheduler compatibility path until Tasks supports routed child wakes. See chat-shared-layer.md and rfc-chat-recovery-foundation.md.
submitMessages() adds durable acceptance with idempotency keys and status inspection; see think-durable-submissions.md.
The browser sends tool schemas with the chat request. Think converts them with createToolsFromClientSchemas() and merges them last. The latest schemas and request body are persisted in think_config (lastClientTools, lastBody) so they survive hibernation and apply to auto-continuations.
Tool results and approvals update the matching tool part through the shared applyToolUpdate helpers, persist it, and broadcast cf_agent_message_updated. When the client asks to continue, the shared AutoContinuationController coalesces results for 50 ms and then runs one continuation turn.
Think speaks the same protocol as AIChatAgent; the constants are CHAT_MESSAGE_TYPES in agents/chat.
| Direction | Message type | Purpose |
|---|---|---|
| Client → Server | cf_agent_use_chat_request |
Send a chat request ({ messages, clientTools?, ...body }) |
| Client → Server | cf_agent_chat_clear |
Clear the conversation |
| Client → Server | cf_agent_chat_request_cancel |
Cancel a request by ID, or the continuation this tab waits on |
| Client → Server | cf_agent_tool_result |
Client tool result |
| Client → Server | cf_agent_tool_approval |
Tool approval or denial |
| Client → Server | cf_agent_stream_resume_request |
Ask whether a stream can be resumed |
| Client → Server | cf_agent_stream_resume_ack |
Acknowledge a resumable stream and receive the replay |
| Server → Client | cf_agent_use_chat_response |
Stream chunk (done: false) or terminal frame (done: true) |
| Server → Client | cf_agent_chat_messages |
Full transcript broadcast |
| Server → Client | cf_agent_chat_clear |
Conversation cleared |
| Server → Client | cf_agent_message_updated |
One message changed (tool result or approval applied) |
| Server → Client | cf_agent_stream_resuming |
An active stream can be resumed |
| Server → Client | cf_agent_stream_pending |
A turn is accepted but has not started streaming |
| Server → Client | cf_agent_stream_resume_none |
Nothing to resume |
| Server → Client | cf_agent_chat_recovering |
A durable turn is being recovered |
useAgentChat from @cloudflare/think/react wraps the shared hook in agents/chat/react. It omits syncMessagesToServer, because the Session tree is server-authoritative; setMessages changes only the local view and clearHistory() performs a persisted clear.
configure() / getConfig() store a JSON-serializable blob under _think_config in the Think-private think_config table and cache it in memory. Legacy keys from assistant_config are lifted into think_config on startup. A parent can configure a child over RPC. Values that should reach clients belong in Agent state instead; configure stays server-side.
A Think instance holds one conversation on the default Sessions handle. Multi-chat products compose Durable Objects: a per-user directory owns the chat list and shared resources (workspace, MCP), and each conversation is its own Think instance. The former examples/assistant implemented this with facet children; rfc-user-chat-durable-objects.md records the accepted move to top-level chat Durable Objects. The user guide is docs/think/multi-chat.md.
this.workspace is typed ThinkWorkspace, which needs only workspace.fs. By default it is a Workspace from @cloudflare/computer over the object's SQLite, with no execution backends; a subclass can assign its own, for example one with backends, or a client from getWorkspace(parentStub) for a parent-owned workspace. Think's internals (media eviction, fetch spill, extension host functions) go through one adapter over workspace.fs, and Think writes its own files under /workspace. Computer's workspace tools (read, ls, find, grep, write, edit, delete, plus exec when the Workspace has backends and publish when it has assets) are merged into every turn. There is no Think option for them: a same-name tool from getTools() replaces a built-in, and activeTools from beforeTurn hides tools for a turn. Every Think agent implements __getWorkspaceStub(), so getWorkspace(stub) and the Worker shell backend can reach a Workspace it owns.
createExecuteTool(this, overrides?) builds a codemode runtime from the agent (ctx, env.LOADER, optional env.BROWSER) and returns the execute tool. The model writes JavaScript that runs in a Dynamic Worker with tools.* and cdp.* connectors; pass createAITools({ workspace: this.workspace }) as tools to reach the workspace. The runtime is assigned to this.codemode, which backs the approveExecution / rejectExecution / pendingExecutions callables for approval-gated sandbox tools. See think-execute-hitl.md.
Opt-in, read-only HTTP reads. createFetchTools() generates a generic fetch_url tool (when a public allowlist is set) plus one fetch_<name> per binding target. The fetchTools property wires them in after workspace tools, injecting this.workspace for spill and a tool:fetch observability event.
Key decisions: named tools so per-target policy is baked in; GET only, because mutations belong in approval-gated actions and recovery can replay a turn; SSRF defenses for the public path (private, loopback, link-local, and *.internal targets blocked, credentials rejected, IPv4 shorthand normalized); maxBytes, maxModelChars, and workspace spill as size knobs; allowlist-aware redirects with cross-origin header stripping; results returned as { ok, ... } values rather than thrown.
Re-exports from agents/browser. createBrowserTools() provides the durable browser_execute tool (CDP through a codemode runtime; needs a Browser Run binding and a Worker Loader) and, by default, the stateless Quick Action tools. createQuickActionTools() provides only the Quick Actions (browser_markdown, browser_extract, browser_links, browser_scrape, and optionally browser_content) and needs only the browser binding.
createSandboxTools() is a placeholder: it logs a warning once and returns no tools.
With extensionLoader set, Think creates an ExtensionManager (@cloudflare/think/extensions):
- Loading wraps extension source in a Worker module with
describe()/execute()RPC and loads it throughWorkerLoaderwith permission-gated bindings. - Tool discovery exposes the described tools as AI SDK tools named
{extensionName}_{toolName}. - Persistence stores the manifest and source in Durable Object storage;
restore()rebuilds them after hibernation. - Permissions declare
networkhosts andworkspaceaccess (read,read-write, ornone). Workspace access goes throughHostBridgeLoopback, aWorkerEntrypointthat resolves the agent throughctx.exports.
createExtensionTools() (@cloudflare/think/tools/extensions) gives the model load_extension and list_extensions. getExtensions() declares extensions in code, and extensions can contribute lifecycle hooks.
These have their own user docs under docs/think/:
- Actions (
getActions(),action()): tools with an idempotency ledger, durable-pause approvals, and authorization. The ledger and the approvals are Task runs; see Actions on Tasks. - Channels and messengers: per-surface policy (
configureChannels()) and Chat SDK webhook ingress with durable reply delivery. - Scheduled tasks:
getScheduledTasks()mapped onto the Scheduler's declared schedules (declareSchedules(), internal, see declared-schedules.md) on startup, on the root agent only. Prompt tasks become durable submissions. - Agent tools: running Think or
AIChatAgentchildren as tools, with run state incf_agent_tool_child_runsandcf_agent_tool_milestones. See agent-tools.md. - Skills:
getSkills()andgetSkillScriptRunner()overagents/skills;skills.workspace(this.workspace)reads skills from Workspace files. See skills.md.
Actions can record advisory delivery metadata for the current reply with ctx.attachReply(attachment). The attachment never changes the model-visible tool output, and surfaces that do not understand a type ignore it. Think JSON-normalizes attachments on record, caps how many one turn can record, deep-copies snapshots on read, and exposes the producing attempt's attachments through onChatResponse(result).attachments and replyAttachments(requestId?). Policy callbacks receive a no-op recorder, and attachments from an execute that later fails are discarded. durable-pause approved actions do not carry attachments across the continuation turn. Rendering belongs to channels and voice.
packages/think/src/action-ledger.ts is a module with no storage of its own. It defines two Task definitions; every row is a run in cf_agents_task_runs.
Effects (__cf_think_action_effect). An action call with a ledger key runs as the run __cf_effect:<key>, with input { name, inputHash } (stableHash, the 128-bit value hash Think always used: key order is ignored, a Date hashes by its time). Attempt 1 runs the call's execute from the run's local, at once (start: "now"), and the run's result is its recorded value.
- A later call with the same key, name and input replays the result without running. Another name or input under the key answers
ActionKeyConflict, also while the first call still runs in this isolate; a same-input caller in this isolate shares the running call. - A throw deletes the run, so a later call runs again. A value that is not JSON, or longer than 1,000,000 characters, is returned but not recorded (
action:ledger:serialize_failed), and the run is deleted. An action's output is capped at 20,000 characters before the ledger sees it, so the second case is not reachable from an action. - A run a reset cut off comes back as attempt 2 with no
local: it parksin-doubt, and runs nothing. A caller answersActionPendingError, unless the key is an explicitidempotencyKeyand the claim is older thanactionLedgerPendingRetryLeaseMs: then the run is aborted, deleted and run again (action:ledger:reclaimed). A fallbacktool:<toolCallId>key is never reclaimed. - Retention (
actionLedgerRetention) deletes completed runs and claims left in doubt per their windows, at mostmaxSweepRowsper start. A claim this isolate is running is never swept.
Decisions (__cf_think_action_decision). A durable-pause action parks as the run __cf_decision:<execution id> (run ids are one space per agent, so each kind has its prefix; callers see the execution id), with the action, tool call and model input as input and the approval descriptor as metadata (TaskSettlement.park("decision"), no deadline). pendingApprovals() lists the parked runs. approveExecution / rejectExecution settle the run once with the verdict (tasks.settle); a second answer, or one after another verdict, gets "no longer pending".
- The verdict is stored before anything runs. The run's terminal report (
onStatus, at least once) runs the approved action through the ledger, writes the outcome into the transcript, and continues the chat. Tasks keeps the report job armed until the terminal report returned, and Think's try-counter sync makes it durable before the action starts, so a reset inside it is delivered again by the job about 30 s later with no request; the approved side effect replays from its effect run, or answersActionPendingErrorfor atool:key whose claim is in doubt. In a facet, the job waits for the facet to be called (the facet wake goes through its root). - Each try is counted in the run's metadata and synced before the action runs. After three tries the outcome is an error; if even that write keeps failing, the decision is deleted.
- An approved action whose claim a reset left behind, when the lease lets it be taken over later, does not settle with
ActionPendingError: the report fails, without spending a try, and runs again from the report job. - An outcome that may not have reached the transcript stays on the decision's metadata as
resolvedPauseuntil the pre-inference flush writes it; then the run is deleted. - A codemode approval or rejection calls the codemode runtime once, from
approveExecution/rejectExecution, and records the outcome as a decision run that completes at once. The runtime is never called again after a reset:approveresumes whatever is paused now, so a second call could approve a pause nobody approved. actionPendingApprovalTtlMsexpires parked decisions nobody answered (action:pause:swept).
Memory. EffectState holds one entry per effect running in this isolate, removed when it settles. _decisionOutputs holds one entry per decision settled in this isolate, until its approveExecution call reads it. Resolved pauses waiting for the transcript are loaded once per isolate.
Released data. On start, cf_think_action_ledger rows become effect runs (a settled row as a completed run that replays), cf_think_action_pending_approvals rows are parked again under their ids (their TTL counts from the move), and cf_think_deferred_resolved_pauses entries become completed decisions with resolvedPause. The old table and key are then dropped. The move is one way.
| Table | Owner | Purpose |
|---|---|---|
cf_agents_session_messages / _message_chunks |
Sessions | Message tree and continuation rows for large messages |
cf_agents_session_compactions |
Sessions | Compaction overlays |
cf_agents_session_attachment_meta / _chunks / _refs |
Sessions | Content-addressed media payloads and their references |
cf_agents_session_fts |
Sessions | FTS5 index, created on first search() |
cf_agents_context_blocks |
Context | Context blocks and the frozen system prompt |
cf_agents_streams / cf_agents_stream_blocks |
Streams | Resumable in-flight output |
cf_agents_task_runs / cf_agents_task_steps |
Tasks (inherited) | Chat-turn, recovery, messenger-reply, submission, action-effect and decision runs |
think_config |
Think | configure() blob, client tools, request body, skill state |
cf_agent_tool_child_runs / cf_agent_tool_milestones |
Think | Agent-tool runs |
Think and AIChatAgent share Sessions, Streams, reconciliation, concurrency strategies, durable recovery, programmatic turns, and the client protocol and hook. The remaining differences are intentional:
- Think owns model selection, the tool loop, context assembly, and compaction.
AIChatAgentexposesonChatMessage() → Responseand leaves the model call to the subclass. - Think treats regeneration as a new branch.
AIChatAgentkeeps destructive regeneration andmaxPersistedMessages. AIChatAgentconverts legacy message shapes (autoTransformMessages) and accepts client transcript sync. Think expects current UI messages and is server-authoritative.- Think adds workspace tools, extensions, actions, channels, messengers, submissions, scheduled tasks, workflows, and
chat()RPC.
See think-vs-aichat.md.
The message store, streaming protocol, persistence pipeline, recovery, and error handling are intertwined. A mixin would have to compose with others that also wrap onMessage, onStart, and storage. A base class makes the lifecycle explicit.
Every entry path funnels through one pipeline, so a hook fires the same way for WebSocket, RPC, programmatic, messenger, and recovery turns. Replacing the whole loop would bypass tool merging, recovery, and persistence rules that the other paths depend on.
StreamAccumulator, ResumableStream, TurnQueue, reconciliation, and the recovery engine are shared with AIChatAgent so fixes land once. See chat-shared-layer.md.
Sessions treats an append of an existing ID as a no-op and exposes an explicit update. Retries and reconnects can repeat an append safely, while assistant updates stay deliberate.
Think keeps an in-isolate message array for its model and client paths. The Sessions change feed patches it after durable writes; SQLite remains authoritative across eviction.
Extension Workers loaded through WorkerLoader can only receive Fetcher / ServiceStub bindings, not RpcStub. HostBridgeLoopback carries serializable props and resolves the agent at call time. See loopback.md.
Think is opinionated. It assumes AI SDK UI messages, streamText, and the cf_agent_chat_* protocol. Agents that need a different message format or loop should use AIChatAgent or Agent directly.
Some host paths still materialize arrays. Startup hydration is byte-budgeted, but reconciliation, client transcript snapshots, and model assembly still use arrays. sessions.md records which paths could move to streamed reads.
Reconciliation is host-owned. Client-generated IDs and tool states are wire-protocol concerns, so Think reconciles before writing to Sessions rather than inside storage.
Extension network permission is all-or-nothing. permissions.network declares hosts, but enforcement is binary: no network or full network.
Tests live in packages/think/src/:
tests/— Workers-runtime tests through@cloudflare/vitest-plugin, covering the turn loop, WebSocket protocol, client tools, recovery and eviction, submissions, actions, channels, messengers, scheduled tasks, extensions, tools, and workflows.e2e-tests/— process-level recovery tests againstwrangler dev(chat, submission, workflow, messenger, stall, and action recovery).react-tests/—useAgentChatstream resume.tests-d/and*.test-d.ts— type tests.
| Import path | Purpose |
|---|---|
@cloudflare/think |
Think, Session, Workspace, action, skills, types |
@cloudflare/think/react |
useAgentChat and tool part helpers |
@cloudflare/think/extensions |
ExtensionManager, HostBridgeLoopback |
@cloudflare/think/workflows |
ThinkWorkflow |
@cloudflare/think/messengers |
Messenger helpers and ThinkMessengerStateAgent |
@cloudflare/think/messengers/telegram |
Telegram messenger |
@cloudflare/think/tools/execute |
createExecuteTool, createExecuteRuntime |
@cloudflare/think/tools/fetch |
createFetchTools |
@cloudflare/think/tools/browser |
createBrowserTools, createQuickActionTools |
@cloudflare/think/tools/extensions |
createExtensionTools |
@cloudflare/think/tools/sandbox |
createSandboxTools (placeholder) |
Think's design — skills, extensions, tree-structured sessions, compaction, and context engineering — was inspired by pi, a minimal terminal coding agent by Mario Zechner / Earendil Inc.
- think-roadmap.md — the original five-phase implementation plan (historical)
- chat-shared-layer.md — primitives Think shares with
AIChatAgent - sessions.md and rfc-sessions.md — Sessions as a Lifecycle capability
- rfc-chat-recovery-foundation.md — shared recovery engine
- rfc-think-multi-session.md (rejected) and rfc-user-chat-durable-objects.md (accepted) — multi-chat topology
- loopback.md — extension host bridge
- workspace.md — the Workspace behind Think's file tools