Sub topic 14
Cost, latency, and token impact
This chapter assumes you can call a responses-style API, read tool-call items in a response, and have enabled at least one built-in tool. Built-in tools run on the provider's side, so they add cost and delay that your own code never sees. You need to know where those costs come from before you ship.
Where the cost comes from
A request with a built-in tool has three separate cost sources:
- Tool-call fees. Providers may charge per search call, per code-execution container session, or per file-search query. Prices change, so check the provider's pricing page rather than hard-coding numbers.
- Storage fees. Vector stores used by file search are usually billed by the amount of data stored and how long it is kept.
- Extra tokens. Search results, retrieved chunks, and code output are placed into the model's context. You pay for them as input tokens, even though you never wrote them.
The token cost is the one people miss. One web search can pull thousands of tokens of page text into the context. A follow-up turn that includes the previous output pays for those tokens again.
Where the latency comes from
Each tool call adds a round trip inside the provider. A single search or code run often adds a few seconds. A model that chains several calls, such as search, then search again, then compute, can add much more. The model also has to read the results before it writes the final answer, so time to first token goes up.
Measuring it
Read the usage field on every response and log the time it took. Compare a run with the tool against a run without it.
import time
from openai import OpenAI
client = OpenAI()
def run(tools):
start = time.perf_counter()
response = client.responses.create(
model="gpt-4.1",
tools=tools,
input="What are the most recent stable releases of Python?",
)
seconds = time.perf_counter() - start
calls = [i for i in response.output if i.type == "web_search_call"]
print(f"tools={len(tools)} seconds={seconds:.1f} "
f"input_tokens={response.usage.input_tokens} "
f"output_tokens={response.usage.output_tokens} "
f"search_calls={len(calls)}")
run([])
run([{"type": "web_search"}])Expect the second run to show more input tokens, a longer time, and at least one search call.
Ways to reduce them
- Cap tool use. Set limits such as
max_usesfor Anthropic web search, ormax_num_resultsfor file search. - Retrieve less. Fewer chunks means fewer input tokens. Raise the number only if answers miss facts.
- Steer with instructions. Tell the model to use the tool only when needed. Use
tool_choiceto force or forbid it. - Pick a smaller model for simple lookup tasks.
- Trim history. Do not resend old tool output in every turn.
Common errors
- Turning on every tool "just in case". Each declared tool adds definition tokens and gives the model more chances to spend money.
- Measuring only output tokens. The extra input tokens from tool results are often the larger part of the bill.
- Ignoring storage. Old vector stores keep costing money until you delete them.
Exercise
A request with web search is slower and costs more than the same request without it. Name two places in the response data where you can see why.
Show answer
The usage field shows the extra input tokens from search results. The web_search_call items in response.output show how many searches ran. More calls mean more latency and more fees.