Skip to content
The AI Agent Factory

35.6 Context on a budget, and seeing the cost

Approved by
Panaversity ·
8 min read

In everyday life. A good briefing for a manager is one page with exactly the facts needed for the decision, not the whole file. And a good expense system can show you, for one trip, every taxi, hotel and meal, with one trip number.

What you will learn. How to give each model call the context it needs and no more, and how to follow one assignment's cost and behaviour from end to end.

Why it matters. Input tokens are most of the model bill. And when a client asks "why did this invoice cost $0.40 when the others cost $0.02?", you need an answer in minutes.

Context on a budget

A worker has access to much more than any one call needs: its role, memories, skills, tools, the conversation so far, the invoice, the supplier's history. Sending all of it to every call is expensive, and can make the model worse, because the important facts are buried.

A context planner builds the context for each call in five steps:

  1. Retrieve what may be relevant: the invoice, the PO, the supplier's last three invoices, the relevant skill.
  2. Filter out what the worker may not see, and what this activity does not need.
  3. Rank what is left by usefulness for this activity.
  4. Compress long items: a summary of the supplier history, not every line.
  5. Budget: fit it in the activity's token budget, leaving room for the answer.

Two things are never cut to save money:

  • What the contract and the governance rules require the model to see: its role, its limits, the instruction to stop and ask.
  • The evidence the decision depends on. The paper is direct about this. An economy model's price may rise above 32,000 input tokens. Cutting the invoice's line items to stay under that line, when the match depends on them, saves a fraction of a cent and costs a wrong payment.

Step 5. Add a context planner for the extract and judge activities, with a token budget per activity in spec-platform.md. Required items (role, limits, the invoice and the PO under review) are never dropped. If they do not fit, the call goes to a model with a larger budget, or to a person. Log the token count per activity.

Processes get clean input. A Workflow from Chapter 34 does not need the conversation. Give it structured, checked input: the tenant, the month, the list of invoice IDs. That is cheaper, and it keeps conversation text, which may contain anything a user typed, out of the process's saved state.

One ID for one assignment

Every assignment gets a correlation ID when the control plane creates it. Every record that belongs to the assignment carries it: each model call, each tool call, each Workflow instance and step, each approval, each authorize decision, each usage row.

With that, one query answers "what happened to invoice 1043?": the calls, the models, the tokens, the tools, the approval, the process, and the cost of each.

Step 6. Pass the assignment's correlation ID through every model call (as AI Gateway metadata), every tool, every Workflow start and step, and every usage and decision row. Write a query that, given a correlation ID, lists everything in time order with its cost. Test it on one invoice that needed approval and one that went through reconciliation.

Three kinds of cost

The usage table splits cost the way the paper does:

KindWhat it coversWhy keep it apart
Doing the workModel calls and tools in conversationsThe cost of the service you sell
Running processesWorkflow steps and the model step inside themShows whether delegating pays off
LearningMemory work, reflection, testing new skillsAn investment, judged by what it saves later in this part

Mixing learning into the cost of work makes a worker look expensive in the month it learns most. Keeping it apart lets you see both.

Telemetry is data too. Logs and traces can hold supplier names, amounts and bank details. Keep in them only what you need, redact the rest, and give them the same tenant rules and region as the data they describe. If you send traces to an OpenTelemetry collector, a service that gathers traces from many programs in one standard format, as Part V does in production, that collector is one more store to include in deletion.

The invariant. Give each call the smallest context that keeps every required fact. Tie every cost to one assignment, and keep the cost of learning apart from the cost of work.

Check yourself. A routine invoice cost several times the usual. The correlation query shows 12 model calls, the most the worker is allowed, and most of them came after authorize denied post_invoice. What happened? (The model kept trying a refused payment until it hit the turn limit. The refusal message did not tell it clearly to stop and hand over to a person. Fix the message, and add a test that a refusal ends the attempt.)

  • Building Affordable, Self-Improving AI Workers (Panaversity, 29 November 2026), sections 11.1 and 11.3
Owner
Panaversity