The first time I wired an agent to a data-heavy screen, I gave it the same JSON the table already uses. That felt honest. The UI needs every column, so the model probably does too.
It does not. A list endpoint for a ledger, a log, or a payments grid is shaped for people who scroll. An agent that is trying to answer one question does not need the names, tags, timestamps and metadata that travel with every row. Those fields still become input tokens, on every turn, until something evicts them from the context.
This article is about counting that cost, then changing the tool result, not the model. I built a 1,000-row sample ledger, asked a narrow question, and measured five payload shapes with tiktoken. The encoding is o200k_base, which is what current OpenAI models use. I also checked cl100k_base; the counts were within a few hundred tokens. Claude's tokenizer is not identical, so treat these as measured sizes, not an invoice.
The question, and the naive answer
The question is ordinary:
Posted EUR lines booked in November 2026 with an amount over 10,000 minor units. How many are there, and what are the references?
The naive tool does this:
json.dumps(rows, indent=2) Each row looks like a row a table would render: debit and credit names, a description, tags, a metadata object, created-by, timestamps. That is a reasonable UI payload. It is a poor prompt.
On this sample, that pretty-printed dump is 697,169 characters and 236,366 tokens.
The sample is deterministic (seed=42), so you can reproduce the numbers. Of the 1,000 rows, 46 actually match the question. The model does not need the other 954 in order to answer.
Count first
I used a small helper and five shapes of the same data:
import tiktoken def count_tokens(text: str, encoding_name: str = "o200k_base") -> int: return len(tiktoken.get_encoding(encoding_name).encode(text)) | Shape | Characters | Tokens | Versus pretty JSON |
|---|---|---|---|
| Pretty JSON, all 1,000 rows | 697,169 | 236,366 | 100% |
| Compact JSON, all 1,000 rows | 528,168 | 161,366 | 68.3% |
| Projected columns, all 1,000 rows | 122,318 | 39,329 | 16.6% |
| Server-filtered page of 25, plus totals | 3,238 | 1,028 | 0.4% |
| One projected row | 122 | 40 | — |
Dropping whitespace helps, and it is free. It does not fix the real problem. You are still sending a thousand rows so the model can throw most of them away.
Project the columns the next step needs
If the agent needs a reference, a date, an amount, a currency and a status, do not also send display names, tags and metadata. A projection is a short, explicit list of keys:
PROJECTED_FIELDS = ( "id", "reference", "booked_on", "amount_minor", "currency", "status", ) def project(row: dict) -> dict: return {key: row[key] for key in PROJECTED_FIELDS} On this ledger, projecting every row and compacting the JSON cut the payload from 236,366 tokens to 39,329. That is a real saving, and it is still the wrong shape for this question, because 954 of those rows cannot be the answer.
Keep the full row in your database. Give the tool a narrow view of it.
Filter and page on the server
The cheaper move is the one a data-heavy UI already makes: run the query where the data lives, then return a page and a total. The tool result should say what was asked, how many rows matched, whether more remain, and a short page of projected rows.
def matches_query(row: dict) -> bool: return ( row["currency"] == "EUR" and row["status"] == "posted" and row["booked_on"].startswith("2026-11") and row["amount_minor"] > 10_000 ) def paged_payload(rows: list[dict], *, page_size: int = 25, cursor: int = 0) -> str: matched = [project(r) for r in rows if matches_query(r)] page = matched[cursor : cursor + page_size] next_cursor = cursor + page_size has_more = next_cursor < len(matched) return json.dumps( { "query": { "currency": "EUR", "status": "posted", "booked_on": "2026-11", "amount_minor_gt": 10_000, }, "total": len(matched), "has_more": has_more, "next_cursor": next_cursor if has_more else None, "sum_amount_minor": sum(r["amount_minor"] for r in matched), "rows": page, }, separators=(",", ":"), ) That first page is 1,028 tokens. The second page, which holds the remaining 21 matching rows, is 872 tokens. If the agent fetches both pages, it has seen every matching row for 1,900 tokens. That is 0.8% of the pretty-printed dump.
The echo of query is deliberate. It costs a few tokens and stops the model from answering a slightly different question than the one the server just ran. The totals let it say "46 lines, summing to 5,929,244 minor units" without scanning a thousand objects.
The cursor in this sample is an index into an already filtered, frozen list. That is fine for a measurement. On a live table that other people are writing to, encode a keyset such as (booked_on, id) instead, so a new insert cannot skip a row between pages.
Let the model ask for one row
Once the page has an id, a second tool can return that row alone. In this sample, one projected row is 40 tokens.
That is the pattern I want from an agent on a data-heavy API: a cheap index first, a precise read second. It is the same split I want from a human UI. The table does not render a detail drawer for every row up front.
A TypeScript contract for the tool keeps both sides honest:
type LedgerRow = { id: string; reference: string; booked_on: string; amount_minor: number; currency: string; status: string; }; type LedgerPage = { query: { currency: string; status: string; booked_on: string; amount_minor_gt: number; }; total: number; has_more: boolean; next_cursor: number | null; sum_amount_minor: number; rows: LedgerRow[]; }; When the user changes a filter, start again from the first page. Do not reuse a cursor that was built for a different query. And do not invent page numbers in the UI if the API does not have a stable total in that sense; has_more and total are enough for an agent, and usually enough for a grid.
Give the tool a query, not a "get everything" button
The payload is only half of the contract. The tool's input should look like the filters a data-heavy table already sends to its API, not like fetchLedger(). If the model wants posted EUR lines in November over 10,000, it should pass those values in. The server applies them, the way matches_query does above, and returns a page. The model should not receive a thousand rows and then pretend to be a database. That is the same lesson a data-heavy UI already learned: do not ship the whole dataset to the browser and filter it in a useMemo if the API can do the work.
This also keeps later turns cheaper. A compact page of 25 rows is easier to evict, summarise, or ignore than a 200,000-token blob that now sits in the history. If you later add a mode where the model writes a small script to call tools in a sandbox, the same rule applies: intermediate results can stay out of the conversation, but only if they were never huge to begin with.
Pitfalls
Pretty-printing is not free. indent=2 turned a 161,366-token compact dump into 236,366 tokens. Logs and debug prints are a common way this sneaks into a tool result.
Projection is not a substitute for a query. Sending every row with fewer fields is still sending every row. If the question has a filter, run the filter on the server.
Do not hide the rest. If 46 rows match and you only return 25, say so with total and has_more. A short "here are some results" payload that does not admit it is a page will make the model invent a complete answer.
Be honest about the tokenizer. I measured with tiktoken. If you ship against Claude or another model, count with that vendor's tokenizer before you treat a saving as a budget line. The ranking of these shapes should hold; the exact integers will move.
Do not put the whole table in the system prompt "for reference." A tool result you can page is cheaper than a prefix you pay for on every later turn, even when a cache hits part of it.
Keep the tool list small, or load schemas on demand. This article is about the payload, not the tool catalogue. The catalogue matters too, but shrinking a 236,000-token result comes first.
What I would do next time
If I am attaching an agent to a screen I already know, I would not expose the table's list endpoint as a tool. I would expose a query: filters the server can apply, a projected page, a total, and a way to fetch one row by id. I would count a real response with tiktoken before I shipped it. And I would keep the UI payload and the tool payload as two views of the same data, not one JSON blob that tries to serve both.
The model can still be wrong. It just no longer has to be wrong with a hundred thousand tokens of leftover columns in the way.
I'm Aran Shayan, a frontend developer at SOHA who builds data-heavy interfaces and the APIs behind them. More at aranshayan.com.


![[Kalamos Devlog] After Open-Sourcing: v0.2 to v0.7 Feature Evolution and Using Multi-Agents to Review External PRs](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvo6aurmkktm5betltn9v.png)
