BlogTechnical

Reduce Agent Context Cost by Loading Tools on Demand

Every tool schema injected into a prompt consumes input tokens before the model reads the user's question. Measure that overhead first, then load full schemas only when a task selects them.

1. Measure actual schema tokens before optimizing

Do not estimate overhead from tool count—nested parameters and examples vary wildly. Serialize the exact tool payload for a representative request, count tokens with the target model's tokenizer, and separate fixed tool overhead from user, retrieval, and output tokens.

2. Keep a minimal discovery catalog in initial context

{
  "calculate_lcoe": {
    "description": "Calculate LCOE from submitted project assumptions",
    "input": "project assumptions"
  }
}

3. Preserve strict execution boundaries

  1. 1Select a candidate tool from the compact discovery catalog.
  2. 2Fetch its full instruction set and JSON input schema.
  3. 3Validate arguments against the schema before execution.
  4. 4Run under existing permission scopes and timeouts.
  5. 5Log catalog version, schema version, and validated output.

4. Trade-offs to benchmark on the same eval set

Routing misses

Keep descriptions concrete enough to disambiguate overlapping tools.

Extra round-trip

Measure whether added routing latency offsets token savings.

Schema drift

Version catalog entries and full instructions together.

Auditability

Persist exact catalog and instruction versions in run logs.