Skip to content

Extracting a coding session that holds nothing to remember costs a fraction of a cent - #1676

Merged
zlareb1 merged 3 commits into
mainfrom
fix/extraction-idle-session-cost
Nov 28, 2026
Merged

zlareb1 merged 3 commits into
mainfrom
fix/extraction-idle-session-cost

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Performance Improvement

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

Extracting a coding session that a plugin captured costs under a cent when the provider's prompt cache is cold, where it cost 5 to 9 cents, and about a fifth of a cent when it is warm. A session capture is now read with a prompt of its own, about 2,700 tokens, instead of the 20,000 to 34,000-token conversation prompt.

Why

A measured run of 14 coding sessions with the plugin installed, in which the agent called no Loom tool, ended each session with one server-side extraction call. The provider's usage records for those 14 calls, at list prices of $2 per million input tokens, $2.50 for a five-minute cache write, $0.10 for a cache read and $10 for output:

Calls System prompt Session text Cache Cost per call
3 20,699 tokens 300 to 347 tokens written $0.054
8 20,699 tokens 308 to 396 tokens read $0.0045 to $0.0091
3 34,294 tokens 628 to 767 tokens written $0.090

13 of the 14 captures wrote no memory. The system prompt was about 98% of every request, and 88% of the spend was writing or reading it.

The cause is which prompt a session capture got. It was the conversation prompt, whose rules and 23 or 49 examples are about people, purchases, dates and recommendations, with a block appended that replaced its code rules for a coding session. A capture over 1,200 characters got the full prompt and a shorter one the compact prompt, so session captures kept two prefixes warm, not one. The provider caches a prefix for five minutes, so a capture that arrived later than that after the previous one of its size paid to write the prefix again.

A session capture now has a prompt of its own, the same at every capture size. A capture that is shown existing memories gets the fence note after it, as every extraction does, and only a capture of 1,200 characters or fewer is shown them. So a namespace with no stored memories uses one cached prefix where it used two, and a namespace that holds memories still uses two, each about 2,700 tokens. The prompt holds:

  • the coding-session rules, word for word as they were: what to extract about the code, what not to extract (the session's own edits, the task, test results, what one lookup answers), and the example;
  • what a user states that holds beyond the session (a standing rule, a preference, a decision, a fact about their setup), and that a request for the task alone is not such a statement;
  • the existing-memory rule and a schema with the fields a coding fact uses and the two that end a state the user bounds (valid_to, foresight), ending with the example's fact as JSON.

It is sent without the examples preamble. That preamble makes numbered examples binding and rules out judging whether a case is worth a fact, and this prompt has no numbered examples and asks for that judgement.

Three alternatives were measured or worked out and left out:

  • A deterministic check that skips the call. A sound check for "no user rule and no code fact" skips none of the 14 recorded sessions: each one has a user turn that reads as an instruction and an assistant turn that names a definition and its file. Telling a standing rule from a task instruction, or a reusable claim from a narrated edit, is the judgement the model is asked for. One of the 14 sessions held a fact worth keeping inside a report of its edits, and a check on sentence shape would have dropped it.
  • A one-hour cache lifetime. The provider offers it at twice the input price for the write, where the five-minute write is 1.25 times. For this 2,689-token prompt that is $0.0108 against $0.0067 per write, and a read is $0.00027 either way. It saves $0.0065 on a call that starts 5 to 60 minutes after the previous one and adds $0.0040 to a call that starts later than that, so it pays only where the first kind outnumber 0.62 of the second, and no arrival data says they do. It also raises the cold call this change bounds from $0.0083 to $0.0124, and the cache marker is shared by every model call the server makes: a cold write of the 34,294-token conversation prompt would go from $0.086 to $0.137.
  • The fence note inside the prompt, so that the system text would be the same with and without existing memories and a namespace that holds memories would use one prefix too. Measured on one recorded session that only reports its own edits: with the note and the two validity fields in the prompt it yielded facts in 6 runs of 12, and the verifier kept one or two of them in 3. With the fields alone it yielded none in 12 runs, with the note alone none in 10, with neither none in 6. The note stays where it was, after the prompt of a capture that is shown memories.

Scope and risk

  • Touched: the system prompt of a plugin's session capture (extra_tags with source:claude-code, source:codex or source:cursor). Conversations, trajectory replays, the durability verifier, the retry rules and the coverage sweep are unchanged.
  • Narrower schema. The session prompt asks for key, value, tags, entities, topics, linked_keys, source_message_indices, temporal_anchor, valid_to, foresight, relations, about and forget. It no longer asks for temporal_confidence, valid_at, derived or question_topic, which the parser defaults. The extractor therefore sets no valid_at or occurrence record on a session capture's fact; the row takes its valid_at from the fact's temporal_anchor when that is not in the future, else from the observation date. valid_to and foresight stay, so a state the user bounds ("frozen until 5 December") ends on that day and is not served after it. relations stays, so a fact with two entities does not cost the relation backfill call.
  • What could break: which facts a session capture keeps. The replay below compares the kept facts on recorded sessions. It covers one repository and one model. One capture with a state the user bounds was replayed; no session in it has a user preference, a state that ended in the past (valid_to) or a forget request, so those paths of the new prompt are covered by its text alone.
  • Left alone: the cache lifetime and a pre-call check (above), the thinking effort of the call, and the plugin.

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none. During a rolling upgrade the two builds extract session captures with different prompts and write the same row shape.
  • Rollback: reverting restores the old prompt. Nothing is stored differently.

Tests

Offline replay, no provider call. The 14 recorded captures were rebuilt from the session transcripts with the plugin's turn selection, and the request was built with the server's own functions (_extraction_system_prompt, _build_extraction_user_prompt, the provider client's payload builder). On main the rebuilt requests match the recorded ones byte for byte (65,876 to 66,203 and 103,797 to 104,165 bytes) and token for token, counted with the provider's token-count endpoint. At the head commit:

Before After
System prompt 20,699 tokens (11 captures), 34,294 (3 captures) 2,689 tokens (all 14)
Session text 300 to 767 tokens unchanged
Request body 65,876 to 104,165 bytes 9,420 to 10,652 bytes
Distinct cached prefixes, namespace with no stored memories 2 1
Distinct cached prefixes, namespace that holds memories 2 2

LOOM_ENV_FILE= python -c "from server.memory.domain.extraction_prompts import _CODING_SESSION_SYSTEM_PROMPT as p; print(len(p))" prints 8058 characters.

Paid replay on the configured extraction model, about $0.91 of provider calls in all. Each capture went through the server's own stages (loom.llm_extract, loom.drop_activity_narration, loom.drop_non_durable, loom.verify_durable) with an empty namespace. At the head commit:

  • The 14 idle captures, once each: 13 returned no fact in one call of 9 or 10 output tokens. Warm, they cost $0.0010 to $0.0019. The one cold call at this head held 769 session tokens and cost $0.0084; from the same counts the largest idle capture (767) works out to $0.0083 cold and the smallest (300) to $0.0074. The fourteenth returned one fact, which the verifier kept: the same session is the one that wrote a memory in the recorded run. It cost $0.0057 with the verifier call.
  • A recorded session that holds a rule the user stated and a code path the assistant traced, two runs: the same three facts kept in both (the rule, how the provider is chosen, the call path). On main, two runs: the same three facts.
  • Three recorded sessions that only report their own edits: no fact extracted in 16 runs (12 of one session, ten of them with this prompt's text before it was committed, and two of each of the others). On main, two runs each: one fact extracted in four of six runs and dropped by the verifier each time, no fact kept.
  • One recorded idle capture with a sentence added to the user's turn, "The release branch is frozen until 5 December, so nothing merges there before then.", observation date 28 November, followed through the write step with the store call captured. Before the schema asked for foresight and valid_to, two runs: the fact came back with neither, and the row was semantic with no valid_to. At the head commit, three runs: foresight is {"until": "2026-12-05", "duration_days": null} and the row is foresight with valid_to 2026-12-05 in every run. The sentence is not one of the prompt's examples.
  • The prompt in this commit is the fourth wording. The first two each produced an extraction that this one does not. With the first, one session-only instruction was extracted (the verifier dropped it) and one fact about the changelog was kept in one run of two. With the second, one idle session yielded a fact that restated the task's instructions in four runs of four (the verifier dropped it each time) and two more facts in one of those runs, which the verifier kept; and one session that reports its edits kept a fact about two places that change together in two runs of two. The cause was the layout: the list of what not to extract sat inside the code section and read as applying to code facts only. The user section now says the list applies to every turn, and with the third wording that idle session returned no fact in five runs of five. The third wording is this one without valid_to and foresight; with this one that session returned no fact in its one run.

Not verified: the comparison has two to twelve runs per session, on one repository and one model, so a difference in how often a borderline fact is kept would not show unless it is large. No session with existing memories was replayed, so the existing-memory rule, linked_keys and the prompt followed by the fence note are untested against a model. One bounded statement was replayed, with a date; a stated length ("for two weeks") and a past end (valid_to) were not.

Unit tests. tests/unit/test_coding_session_extraction.py now checks that a session capture's system prompt is the session prompt alone at both capture sizes, with no conversation example and no preamble, and the same prompt followed by the fence note when existing memories are shown; that it stays under 9,000 characters; that the schema names every field the write step acts on (removing the forget, linked_keys, about, valid_to or foresight line fails it, checked by removing each); that the JSON the schema ends with parses to a complete fact; and that a coverage sweep's prompt takes the schema without the rules. No pytest suite was run locally. The bodies of every test in that file (30 cases) and of the sweep-prompt, prompt-fence, package-contract, doc-link and public-copy tests pass when run as a plain script; CI runs the suites.

Gates. ruff format --check, ruff check, the function-length and secrets gates, every generator with --check, Prettier on the changed page: clean. The mypy gate reports five errors in server/llm/vertex_auth.py, a file this change does not touch, from untyped imports in the local environment, and none elsewhere.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code. Checked: that the rebuilt requests equal the recorded ones before the change, the token counts and costs of every replayed call, and the facts each replay kept.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • Local format, lint, type checks and focused tests pass. The full make test-unit and applicable integration results are reported by CI; a full local run is not required before pushing.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code


Note

Medium Risk
Changes extraction behavior and which facts survive for plugin session captures only; conversation and trajectory paths are unchanged, but borderline kept/dropped facts may shift.

Overview
Plugin session captures (source:claude-code, codex, cursor) now use a standalone extraction system prompt (~2,700 tokens) at every capture size, instead of the full or compact conversation prompt (20k–34k tokens) with a coding block appended. That cuts provider input cost on the common case where most captures write no facts.

The wiring changes: _extraction_base_prompt returns _CODING_SESSION_SYSTEM_PROMPT when coding_session is true; the strict-examples preamble is applied only for conversation extracts, not session captures. The new prompt is self-contained—user durable facts, reusable codebase rules, existing-memory dedup, and a narrower output schema—without numbered conversation examples.

The session schema still includes fields the write path needs (including valid_to, foresight, relations, forget); fields like valid_at, temporal_confidence, derived, and question_topic are omitted and rely on parser defaults. Documentation and unit tests assert prompt isolation, size, schema coverage, and parser compatibility with the embedded example JSON.

Reviewed by Cursor Bugbot for commit 69f4042. Bugbot is set up for automated code reviews on this repo. Configure here.

…action of a cent

A coding plugin's session capture was extracted with the conversation prompt and a block appended that replaced its code rules: 20,699 system tokens for a short capture and 34,294 for a longer one, for 300 to 770 tokens of session. Most captures hold nothing to keep, so that prefix was the price of the call, and a capture that arrived after the provider's five-minute prompt cache had expired paid to write it again.

A session capture now has a prompt of its own, about 2,500 tokens at every capture size: the coding-session rules as they were, what a user states that holds beyond the session, the existing-memory rule, and a schema with the fields a coding fact uses. It is sent without the examples preamble, which binds a reply to numbered examples the prompt does not have.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
@vercel

vercel Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Nov 28, 2026 2:14am UTC
loom-telemetry Ready Ready Preview Nov 28, 2026 2:14am UTC

Request Review

The session prompt's schema asks for `valid_to` and `foresight`, the two fields that end a row's validity. Without them a state the user bounds in a coding session ("the release branch is frozen until 5 December") was written as a standing `semantic` memory with no end, because the parser defaults a field the schema does not name. The write step now gets a `foresight` window for it and writes a `foresight` row whose `valid_to` is the stated end. The prompt is about 2,700 tokens.

A unit test names every schema field the write step acts on, so removing `forget`, `linked_keys`, `about`, `valid_to` or `foresight` from the schema fails it, and the prompt test pins the system text of a capture that is shown existing memories: the same prompt followed by the fence note. The feature page says which fields the extractor leaves unset and where the row's `valid_at` then comes from.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 14e8f4c. Configure here.

Signed-off-by: Lareb Zafar <lareb.zafar@clickhouse.com>
@zlareb1
zlareb1 merged commit c23a621 into main Nov 28, 2026
75 of 77 checks passed
@zlareb1
zlareb1 deleted the fix/extraction-idle-session-cost branch November 28, 2026 02: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.

1 participant