Repository navigation
feat(think): record Think's context on the session - #2773
Conversation
🦋 Changeset detectedLatest commit: 4756c62 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
agents
@cloudflare/ai-chat
@cloudflare/codemode
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
🟢 agents import sizes: 1 entry point changed, no growth
Changed exports (1)
How this worksEach 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 |
eed362f to
f10d0cb
Compare
|
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. |
bde8a57 to
7d8485c
Compare
5cb3cf4 to
ca77229
Compare
95e1b7f to
92a8972
Compare
55900a5 to
c3cca6f
Compare
92a8972 to
aab6f4b
Compare
c3cca6f to
0bed47a
Compare
aab6f4b to
b5944be
Compare
0bed47a to
2434ea5
Compare
b5944be to
ce27cd5
Compare
2434ea5 to
67422d3
Compare
ce27cd5 to
d4ed309
Compare
d4ed309 to
a34472b
Compare
67422d3 to
380b88b
Compare
…d reminders, keep regenerations on their prompt, copy records on fork
… sent as first sent
380b88b to
4756c62
Compare
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/contextgetsContext(experimental), which Think'sthis.contextnow is;freezeSystemPrompt,refreshSystemPromptandremindermove there fromContextBlocks.Stacked on #3061 (session annotations, which hold the records: server-side, never in message JSON, deleted with their message, carried by an overlay).
Why:
_system_promptrow, outside the transcript, so a regenerated or windowed turn could be sent a prompt from another branch or a later turn (agents/context: cache the system prompt by default and restore the fluent withContext API #2772).What changed for code on
agents/context0.34: the three prompt methods are onContext;ContextBlockstakes(configs, { defaultProvider? })and addsrenderSystemPrompt().Context.prepare()takesretry: truefor another attempt of a turn already sent. In Think nothing changes:this.contextkeeps its method names,configureContext()andwithContext()work as before, andwithCachedPrompt(provider)is still honoured.Other rules it keeps:
ThinkHarnessafter an interruption), because a first attempt that changed nothing records nothing.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:
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
Contextkeeps 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).