← Back to Work

( Engineering Notes · 2026 · 4 min read )

Building AI Agents That Are Fast and Never Invent Numbers

Why LLM agents get slow and make up figures on real data, and the architecture that fixes both: exact aggregates, dataset handles, reconciliation gates and read-only subagents.

LLM agents are brilliant at deciding what to do next and surprisingly bad at doing arithmetic over real data. The moment you ask an agent for a report over thousands of rows, two problems show up at once: it gets slow, and it starts inventing numbers. This post is about fixing both, because in any business context a fabricated figure is worse than no figure at all. It gets pasted into a slide, then into a decision.

Why this is a real engineering problem

A typical agent runs a loop: think, call a tool, read the result, think again. That loop is fine for small lookups. It falls apart on large datasets for two reasons.

Failure mode 1: the serial agent

If a tool reads data in pages of a few hundred rows and each page takes a few seconds, a 12,000-row dataset takes many minutes. Worse, every later model call carries that whole dump in its context, so each step gets slower and more expensive. And because the model cannot tell whether a long-running call succeeded, it often repeats the exact same call.

Failure mode 2: the confident estimator

The model rarely sees the full dataset. It sees a few chunks, then confidently produces totals, percentages and categories that look right and are not. Ask it for the sum of a column across a 3,000-row spreadsheet and it will happily give you a number that is off by two orders of magnitude.

Neither failure is the model's fault. They are architecture problems, and they have architecture fixes.

How to build agents that are fast and grounded

1. Move aggregation out of the model

The model should decide which question to ask. Code should compute the answer. Give the agent tools that return exact aggregates (counts, sums, group-bys, filters) computed in the database or in a compute layer, never sampled.

@tool
def task_report(filters: TaskFilters, group_by: list[str]) -> DatasetHandle:
    """Exact aggregates computed in the database. Never sampled, never estimated."""
    rows = db.aggregate(filters=filters, group_by=group_by)
    return datasets.publish(rows)

One well-designed aggregate endpoint can replace hundreds of paginated reads. It is the single biggest speed and accuracy win.

2. Pass handles, not numbers

Instead of dumping rows into the model's context, return a dataset handle: an id, the schema, the row count and a few preview rows. Render tables and charts on the server from the handle. The model writes "show dataset ds_42 as a bar chart", not 400 numbers. Re-typing is exactly where digits get transposed, so do not let the model re-type.

3. Put a reconciliation gate in front of every save

Before any generated artifact is saved, extract every number in it and check each one against the data actually retrieved in that turn, with a small tolerance for rounding and percentages. If a figure does not reconcile, reject the save and tell the model which number failed and why.

def unsupported_numbers(artifact: list[float], evidence: list[float], tol: float = 0.005) -> list[float]:
    def close(a: float, b: float) -> bool:
        return abs(a - b) <= tol * max(1.0, abs(b))
    return [n for n in artifact if not any(close(n, e) for e in evidence)]

bad = unsupported_numbers(extract_numbers(draft), turn_evidence)
if bad:
    raise ToolError(f"These figures are not supported by the retrieved data: {bad}")

Ship it in warn mode first, measure how often it fires, then switch to enforce.

4. Fan out with read-only subagents

For multi-part questions, let a lead agent write short briefs and hand them to subagents that run in parallel. Wall-clock time becomes the slowest branch instead of the sum of all branches, and each subagent keeps a small, focused context.

Rules that keep subagents safe:

  • Subagents get read-only tools. Only the lead agent writes.
  • Cap the fan-out (four at a time is a good default) and give every subagent a timeout.
  • Subagents return structured findings and dataset handles, not essays.
  • The lead verifies findings before composing the final answer.
  • 5. Teach the model your domain

    Many "hallucinations" are really definitional errors. If the model does not know how your business defines a KPI, it will invent a plausible definition and compute that. A short domain primer in the system prompt (what each status means, how each KPI is calculated) removes a whole class of errors. Pin it with tests so it cannot silently drift.

    6. Show progress and keep internal calls internal

    Long tools should stream progress ("Reading records: 3,000 of 12,000") so users see work happening instead of a spinner. Any internal LLM call, such as a classifier or planner, must be tagged so its tokens never leak into the user-facing stream.

    How to know it is working

  • Keep a set of golden questions with known answers and run them in CI.
  • Track wall-clock time per report, tool calls per run, tokens per run, and the percentage of saves the reconciliation gate rejects.
  • Watch for repeated identical tool calls. They usually mean a tool is too slow or returns too little feedback.
  • In one system I worked on, these changes took a 28-minute report down to about 40 seconds and corrected a headline KPI the model had been confidently inventing.

    Checklist

  • Aggregates come from code, not from the model.
  • The model passes dataset handles; the server renders the numbers.
  • Every saved number reconciles against retrieved evidence.
  • Subagents are read-only, bounded and return structured output.
  • Domain definitions live in the prompt and are covered by tests.
  • Long tools stream progress; internal LLM calls never reach the user.