Skip to content

Counting savings from request-rewriting plugins (context-guru, tool-prune): open questions #1417

Open

Description

@huang195

Cortex records what every LLM request costs, and a growing set of plugins, context-guru and tool-prune among them, rewrite requests so they cost less. We would like Cortex to report the savings those plugins deliver in a way operators can rely on. Getting that right touches both projects, so we would like to work it out together with the context-guru team.

This issue collects the open questions as we currently understand them. It deliberately stops short of proposing a design, so we can start from a shared picture of the problem. Corrections and additions are very welcome, especially where we have misread how context-guru works.

Terms

  • Spend: what a request actually cost. Cortex prices it from the token counts in the provider's response.
  • Saving: what a request would have cost without a plugin, minus what it did cost. The request that was never sent cannot be measured, so every saving rests on a model. We call that model the baseline.
  • Cache tiers: providers bill prompt tokens differently depending on the prompt cache. A cache read costs about 0.1x the normal input rate and a cache write about 1.25x, so where a token lands can change its price by 12.5x.
  • Overhead: what a plugin spends itself to produce a saving, for example its own LLM calls.

Where things stand

  1. Spend is priced in one place, core/cost/settle, from Cortex's rate tables, and written to the cost ledger.
  2. Savings are priced in the same place, today for tool-prune only. tool-prune publishes plain facts: bytes removed, body size, and whether the change was applied. settle.Avoided (core/cost/settle/avoided.go) converts bytes to tokens using the request's own prompt-tokens-per-byte ratio, prices them at the cache tier the request landed in, and marks the figure as an estimate. Plugins join this record through an explicit list (savingComponents).
  3. The context-guru plugin in Cortex does not report savings yet. It rewrites the body and logs tokens before and after, but Cortex has nothing to price, so its contribution currently shows as $0.
  4. context-guru's standalone proxy already has a thoughtful savings model (dash/event.go, Event.Price). It separates content removed for the first time from content that is only re-sent on later turns, adjusts tokenizer counts to what providers bill, and nets out its own LLM calls and keep-alive pings. It has worked through several of the questions below in practice.

Open questions

  1. Keeping one set of numbers. Spend is priced from Cortex's rate tables, and context-guru keeps its own. If both price parts of the same request, one request can end up with figures based on different rates. Rate tables drift easily (Cortex has had its own share of missing or stale model entries), so it would help to agree on a single source.

  2. A figure operators can trust. Because a saving is a counterfactual, its value depends on the model behind it. We would like an operator reading "saved $X" to know how it was computed and who stands behind it. That holds for any plugin, Cortex's own included.

  3. Effects beyond bytes removed. Cortex's current model knows only "N bytes removed, priced at this request's tier". Several things context-guru does fall outside that:

    • Cache effects. Rewriting earlier history can change whether a request reads from the prompt cache or writes to it. Spend is still recorded correctly. The saving, though, is priced at the tier the rewritten request landed in. When a rewrite causes a cache write, the saving is therefore priced at the write rate, and it can look largest on exactly the requests where the rewrite cost the most.
    • The plugin's own LLM calls. The summarizer, and the reuse of the agent's own model (model.source: incoming), go out through core/llmclient. By design that client does not pass through the Cortex listener, so these calls are not in the ledger. The ibac judge uses the same client, so this is not specific to context-guru.
    • Keep-alive (context-guru#381). A ping costs a little now to avoid a cache rebuild later. The cost and the benefit land on different requests, and the benefit only materializes if a real request arrives after the cache would otherwise have expired.
  4. Content re-sent every turn. Agents re-send the whole conversation each turn, so content removed once appears removed on every later turn. context-guru's dashboard notes that pricing each turn naively overcounts by 13.1x on one measured window. For tool-prune, pricing every turn is right, because the tools really would have been re-sent each time. For history compaction it is not. Telling the two apart requires knowing which removed content is new this turn, and today only the context-guru engine has that information.

  5. Two ways of counting tokens. Cortex estimates tokens from a byte ratio calibrated on each response. context-guru counts with a tokenizer (o200k_base) and applies a correction factor per model family. The same removal can come out as different token counts.

  6. Credit across several plugins. Request rewriters run in a chain: tool-prune and context-guru can both shrink one request, and each sees what the previous one left. The framework records the body the client sent, the body that went upstream, and which plugins wrote it. It does not record how much each plugin removed, so splitting credit between them is still open.

  7. Observe mode. Under on_error: observe a plugin's change is computed but not applied. Any saving from it is a "would have saved" figure that should stay separate from money actually not spent. tool-prune marks these as Projected, and the same distinction will matter for any new source of savings.

Questions we would like to explore together

  • What should an operator be able to rely on when they see a savings figure?
  • Which facts only the context-guru engine can know, and which can Cortex observe directly?
  • How should overhead (a plugin's own calls, keep-alive pings) appear next to the savings it pays for?
  • Where should the baseline model live, and how should changes to it be reviewed?

Looking forward to the discussion.

Activity

  1. huang195 commented on Oct 19, 2026

    @huang195
    MemberAuthor

    @boazdavid @amiddavid @OsherElhadad, thank you for the context operations proposal and its worked example (rossoctl/context-guru#592).

    It's careful work, and the worked example made it easy to follow. It answers the questions in this issue more completely than we hoped.

    What we'd like to keep

    Most of the model, as written:

    • one operation per change, with a stable ChangeID and FirstApplied
    • each turn priced from its own cache state, relative to B*
    • cache breaks attributed by comparing the client body with the forwarded body
    • a keep-alive ping joined with the request it rescued, so an unused window shows as a loss
    • payback per change and totals per thread
    • test cases built from today's figures before anything switches

    The question: where the code lives

    For Cortex, cost and spend are a core part of the product. Operators read those numbers directly, and we change that code often. If the tier rules and the Operation type live in context-guru, fixing a wrong figure in Cortex takes a PR and release in context-guru, then an upgrade here. We'd like to fix our own numbers on our own schedule.

    A split we'd like to propose

    • context-guru reports what the engine did. apply.Result lists each change: where it is, its size before and after, a stable ID, and whether it is new or a replay. This is step 3 of the spec, unchanged.
    • Cortex owns the operation type, the collector and the pricing. Its contextguru plugin maps the engine's facts into Cortex's types, as it already does for the dispatcher and the keep-alive emitter.

    For your team, that is step 3 and none of the rest. There is no separate ops module to release, no rework of the proxy's dashboard, and no review of Cortex's pricing changes. Your proxy keeps its own savings rules, so the status line and /context-guru:insights stay as they are.

    Keeping context-guru's pricing yours to steer

    We don't want Cortex to price your work in a way you disagree with. So we'd like to:

    • keep the context-guru pricing in Cortex in its own files, with you as code owners
    • give you write access to Cortex, and nominate you as maintainers if you'd like
    • run your test cases in Cortex's CI, with a written reason for any difference

    What this gives up

    The proxy and Cortex would no longer share one rule, so their token counts could differ slightly. Their dollars already differ, because the rate tables do, so we think the loss is small. Is there a case where the two need to agree token for token?

    Happy to discuss here or on a call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions