Skip to content

feat(think): record Think's context on the session - #2773

Merged
mattzcarey merged 9 commits into
mainfrom
feat/session-context
Nov 24, 2026
Merged

mattzcarey merged 9 commits into
mainfrom
feat/session-context

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Think's context is now recorded on the session. Each turn opens with exactly the system prompt it was sent before, and a changed whenChanged: "remind" block is sent once, after the user message of the turn that noticed it, and stays there, instead of moving to the newest user message every turn. agents/context gets Context (experimental), which Think's this.context now is; freezeSystemPrompt, refreshSystemPrompt and reminder move there from ContextBlocks.

Stacked on #3061 (session annotations, which hold the records: server-side, never in message JSON, deleted with their message, carried by an overlay).

Why:

What changed for code on agents/context 0.34: the three prompt methods are on Context; ContextBlocks takes (configs, { defaultProvider? }) and adds renderSystemPrompt(). Context.prepare() takes retry: true for another attempt of a turn already sent. In Think nothing changes: this.context keeps its method names, configureContext() and withContext() work as before, and withCachedPrompt(provider) is still honoured.

Other rules it keeps:

  • A recovered or retried turn is sent exactly as its first attempt was; a change or refresh in between waits for the next user message. The host says which requests are retries (Think from the turn's recovery trigger, ThinkHarness after an interruption), because a first attempt that changed nothing records nothing.
  • When a compaction summary hides the turn that delivered a value, the next request restates it once, right after the summary.
  • A refresh drops reminders recorded before it; a regeneration keeps its branch's prompt while a refresh waits; a fork keeps the context of its copied turns.

Upgrade, measured on a deployment (Worker slim-context-2773): 14 live sessions created on [email protected] / [email protected] (frozen prompt, reminder sent, reminder pending, compacted, memory written, refresh pending, custom prompt store; root and facet) each sent, on their first turn after the head was deployed over them, exactly the bytes the release sends for that turn. The prompt the release froze is carried forward, so the upgrade costs no cache miss.

Prompt cache, exact longest common prefix between consecutive requests over 12 turns (remind block changes at 3 and 8, memory write at 5, refresh at 10), bytes not shared with the previous request:

turn 2 3 4 5 6 7 8 9 10 11 12
0.34 143 456 457 466 464 460 468 473 2,185 153 132
this 143 456 139 148 146 142 466 149 2,823 153 132

With a real provider (Workers AI @cf/deepseek-ai/deepseek-v4.1-flash-1017, which reports cache reads, the same 12 turns plus a regeneration, a ~6k-token prompt, 3 or more runs per side): the release's request stops extending the previous one on turns 4 to 10 in every run, this PR's only at the refresh; provider cache over turns 2 to 12 is 92.8 to 97.1% on 0.34 and 94.9 to 97.8% here. The prompt is large and the reminder small, so the ratio moves less than the exact prefix above.

Cost through the Sessions gate (report-sessions.md, #3061 at d4ed309): a turn reads 57-58 rows against 56; the first turn after a restart reads 73 (empty session) or 121 (100,000 rows) against 56; each context change stores one annotation of about 350 bytes (not one per turn), and clearMessages() removes them. One cost to know: with a context record present, compact() on a 100,000-row session reads 600,167 rows against 200,216, from the Sessions walk that carries annotations onto the summary; that is a Sessions follow-up.

Cost, measured on the deployment against the stack base (main + #3061), 10 identical turns: rows read per turn 59 vs 57 at the root and 47 vs 45 in a facet, flat over the conversation (the record lookup for the turn's two new messages); rows written equal; one statement fewer per turn. A Context keeps the records it has read, bounded to the last window, so a turn reads only its new messages. A turn that delivers nothing new writes nothing; a change turn writes one annotation; a refresh writes one snapshot (the rendered prompt) only if it changed.

Tests: invariant suites on a real Durable Object and Session (a window agrees with the full path; each request extends the previous one except at a refresh, compaction or branch switch; a change is delivered once per branch), seeded over random turns, changes, refreshes, regenerations and compactions; Think's suite; the upgrade and fail-after-record cases through Think. On the deployment, a seeded ctx.abort() before each context and session write (8 runs, root and facet) and hibernation wakes kept all four invariants.

Docs: cloudflare/cloudflare-docs#35550 (Think's Context page).

@changeset-bot

changeset-bot Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4756c62

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
agents Minor
@cloudflare/think Minor
@cloudflare/agent-think Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@2773

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@2773

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@2773

hono-agents

npm i https://pkg.pr.new/hono-agents@2773

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@2773

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@2773

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@2773

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@2773

commit: 4756c62

devin-ai-integration[bot]

This comment was marked as resolved.

@mattzcarey mattzcarey changed the title feat(sessions): declare context blocks on the session handle feat(context): bind context blocks to a session, recorded in the transcript Oct 29, 2026
@agent-think

agent-think Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

🟢 agents import sizes: 1 entry point changed, no growth

Entry point Exports Largest gzip change Size now
🆕 agents/context 1 new — 95.6 KiB
Changed exports (1)
Import Gzip change Size now
🆕 agents/context#Context — 95.6 KiB
How this works

Each runtime export is bundled on its own, minified, and gzipped. Changes smaller than 100 B, or smaller than 1% and 1 KiB, are ignored. Growth over 10% or 5 KiB is marked 🔴. This report is informational and does not fail CI. The workflow artifact contains every measurement.

Compared 4978fb18 → 4756c62f · workflow run · reported by agent-think[bot]

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration[bot]

This comment was marked as resolved.

@dodevs

dodevs commented Nov 16, 2026

Copy link
Copy Markdown

We run Think in production and replaced the moving reminder with our own anchored updates for exactly this reason: measured on a real conversation, one context change left a turn reading 2,688 of 17,519 prompt tokens from cache, and the loss repeated every turn until the conversation went cold. Anchoring the update to the message that first saw it fixed it. We'd move back to native context once this lands; +1 to recording the first turn's snapshot and never moving a delivered change.

mattzcarey added a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 23, 2026
mattzcarey added a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 23, 2026
mattzcarey added a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 23, 2026
@mattzcarey
mattzcarey changed the base branch from main to slim/sessions-annotations November 23, 2026 17:26
@mattzcarey
mattzcarey marked this pull request as draft November 23, 2026 17:26
@mattzcarey mattzcarey changed the title feat(context): bind context blocks to a session, recorded in the transcript feat(think): record Think's context on the session Nov 23, 2026

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 8 new potential issues.

Devin Review

Comment thread packages/agents/src/context/context.ts Resolved
Comment thread packages/agents/src/context/context.ts Resolved
Comment thread packages/agents/src/context/context.ts Resolved
Comment thread packages/agents/src/context/context.ts Resolved
Comment thread packages/think/src/think.ts Resolved
Comment thread packages/think/src/harness/think.ts Resolved
Comment thread docs/agents/context.md Resolved
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from 5cb3cf4 to ca77229 Compare November 23, 2026 17:35
mattzcarey added a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 23, 2026
mattzcarey added a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 23, 2026
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch 2 times, most recently from 95e1b7f to 92a8972 Compare November 24, 2026 06:20
@mattzcarey
mattzcarey force-pushed the feat/session-context branch 2 times, most recently from 55900a5 to c3cca6f Compare November 24, 2026 06:33
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from 92a8972 to aab6f4b Compare November 24, 2026 06:34
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from aab6f4b to b5944be Compare November 24, 2026 07:01
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from b5944be to ce27cd5 Compare November 24, 2026 10:23
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from ce27cd5 to d4ed309 Compare November 24, 2026 10:46
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from d4ed309 to a34472b Compare November 24, 2026 10:56
Base automatically changed from slim/sessions-annotations to main November 24, 2026 11:07
@mattzcarey
mattzcarey merged commit 1975aa4 into main Nov 24, 2026
26 checks passed
@mattzcarey
mattzcarey deleted the feat/session-context branch November 24, 2026 11:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants