Logo
All posts

Speed hacks from OpenAI

In this blog we will see three speed hacks OpenAI uses in ChatGPT and Codex, and the code behind each of them.

Gaurav Sen/12 min read

In 2022, ChatGPT answered one question with one reply. You typed, the model thought, and it answered.

By 2026, one request to an AI agent became dozens of calls to the model. Each call waits on the network, the checks around the model and the model itself.

The five requests of one Codex task, fix the failing test in this GitHub issue, and where each one spends its time. Change the model's speed, the connection, the tools and what happens before you send, and see what each request carries, what the server keeps and what it computes again. Then race your loop against the plain one in real time. The token counts and timings are illustrative.

Research from Amazon have shown that for every 100 milliseconds of added latency, Amazon noted a 1% drop in sales.

Engineers building agents face two problems:

  1. Every call to the model resends the whole conversation, and the server reads all of it again.
  2. The model waits for the user to finish typing, and then the user waits for the model.

In this blog we will see three speed hacks OpenAI uses in ChatGPT and Codex, and the code behind each of them.

1. The agent loop

Where does an agent spend its time? In a loop of calls to the model.

The loop starts when you ask Codex to fix the failing test in a GitHub issue. Codex sends the task to the model.

The model can't run anything. It answers with a tool to call.

The first tool is rg, to search the code. Codex runs it and sends the output back as a new request.

The model asks for sed, then apply_patch, then pytest. Each step is a full round trip to OpenAI's Responses API.

Codex sends the task, fix the failing test in this GitHub issue, to the LLM. The LLM asks for one tool at a time: rg, sed, apply_patch and pytest. Each tool's output goes back as a new request, until the LLM answers that the bug has been fixed.
Codex sends the task, fix the failing test in this GitHub issue, to the LLM. The LLM asks for one tool at a time: rg, sed, apply_patch and pytest. Each tool's output goes back as a new request, until the LLM answers that the bug has been fixed.

This back and forth is called the agent loop. OpenAI says Codex makes dozens of these requests for one task.[1]

In 2026, OpenAI's GPT-5.3-Codex-Spark ran at over 1,000 tokens a second. Earlier flagship models ran at about 65.[1]

Once the model got that fast, the model was no longer the slow part. The time went to the API: validating, tokenizing and safety checking each request.

2. Start before the user asks

How do you save time before the first request? You do the work while the user is still typing.

When you paste a link into ChatGPT's text box, it starts loading the page in the background. So even before you type your query, ChatGPT gains context by preloading the link, vectorizing its data, and warming up its KV cache. The resultant responses feel instantaneous.

You paste a link. While you type your query, ChatGPT loads the page, vectorizes it and warms its KV cache. When you send, the answer comes back at once.
You paste a link. While you type your query, ChatGPT loads the page, vectorizes it and warms its KV cache. When you send, the answer comes back at once.

Doing the work before it is asked for is called preloading. The idea is older than LLMs.

Instagram did this more than 10 years ago. Whenever you upload a photo or a video like this one, Instagram continues loading the video in the background while you're adding the description.

Codex preloads too. Before the first request of a turn, it sends a response.create with generate=false.[3]

The server reads the instructions and the tools, and generates nothing. When your task arrives, it is added to what the server already holds.

3. Send only what is new

Why does the fifth call of the loop cost more than the first? Because over HTTP, each request carries everything before it.

Request 5 carries the task, the four tool calls and their outputs. The server validates and tokenizes all of it again. Only the pytest output is new.

To stop reading it all again, OpenAI kept one connection open for the whole loop, a WebSocket. The server keeps a cache tied to that connection.[1]

Over HTTP, request 5 carries the task and every turn before the pytest result, and the server reads all of it again. Over a WebSocket, request 5 carries the previous response's id and the pytest result. The server keeps the rest in memory.
Over HTTP, request 5 carries the task and every turn before the pytest result, and the server reads all of it again. Over a WebSocket, request 5 carries the previous response's id and the pytest result. The server keeps the rest in memory.

The cache holds the previous response, the tool definitions and the tokens already rendered. So each new request does less work:

  1. The safety checks and validators read only the new input.
  2. New tokens are appended to the cached ones. Nothing is tokenized twice.
  3. The routing decision for the model is reused.
  4. Billing runs alongside the next request.

Codex decides what is new in get_incremental_items.[3] It compares the request with the last one, and sends only the items after them, with previous_response_id.

If anything else changed, such as the model or the tools, it sends the whole request. A request too big for the socket falls back to HTTP at once.[4]

What did it get? OpenAI measured agent loops 40% faster end to end.[1] Cline's multi-file workflows got 39% faster, and OpenAI models in Cursor up to 30% faster.

4. Keep the prefix the same

Is the full tool list resent on every call? A learner asked this in our cohort's live class of 5 September 2026.

An agent connected to a few MCP servers can have hundreds of tools. Sending every definition with every request bloats the context.

The way that we tried to avoid this was to pick up only those tools that are required for this particular query and send them to the large language model. But the problem here was that the KV cache, the cached tokens used to get messed up because the tools that we are using per request were changing.

A model provider caches the start of a request it has seen before. That start is called the prefix, and reusing its keys and values is called prefix caching.

The tools sit near the top of the prefix. Change one tool, and everything after it is computed again.

Request 1 sends the system prompt and three tools, and the KV cache stores them. Request 2 picks a different third tool. From that tool on, the prefix no longer matches, and the cache is computed again.
Request 1 sends the system prompt and three tools, and the KV cache stores them. Request 2 picks a different third tool. From that tool on, the prefix no longer matches, and the cache is computed again.

So, the cache would keep getting invalidated. The prefix cache was very small then, just the system prompt.

So the agent gets one fixed tool that finds the others. Anthropic introduced it in November 2025 as the Tool Search Tool.[5] OpenAI's Responses API added tool_search for GPT-5.4 and later models.[2]

This is like a meta tool which can search for other tools in real time.

In the API, a tool marked defer_loading is left out of the prompt. The model calls tool_search, and the tools it finds are loaded after the prefix.[2]

Codex ranks its tools with BM25, a keyword search, and returns 8 by default.[3] For our GitHub issue, it finds get_issue and create_pr.

Codex's context holds the system prompt and one tool, tool_search. The search ranks the tool registry with BM25 and finds get_issue and create_pr. Only those two are loaded, after the prefix.
Codex's context holds the system prompt and one tool, tool_search. The search ranks the tool registry with BM25 and finds get_issue and create_pr. Only those two are loaded, after the prefix.

The system prompt and tool_search never change. The prefix stays cached for the whole loop. With tool search, Anthropic measured the context its tools used falling by 85%.[5]

Our post on Harness Engineering runs tool search in code.

It's a mix of customer experience and engineering.

Summary

  1. An agent makes dozens of calls to the model for one task. With a model at 1,000 tokens a second, the API overhead became the slow part.
  2. ChatGPT loads a pasted link while you type, and Codex sends the instructions and tools with generate=false before the first request.
  3. Over a WebSocket, Codex sends only the new items with previous_response_id. The server keeps the rest in memory, and agent loops got 40% faster.
  4. Changing the tools per request breaks the cached prefix. Tool search keeps the prefix fixed and loads only the tools a task needs.

Sources

  1. Speeding up agentic workflows with WebSockets, Brian Yu and Ashwin Nathan, OpenAI, 22 April 2026.
  2. Tool search, OpenAI API documentation, read 30 November 2026.
  3. openai/codex, commit 39e7013b, 30 November 2026. The prewarm and the incremental request are in client.rs. The BM25 search is in tool_search.rs, its limit of 8 in tool_discovery.rs.
  4. Fall back to HTTP immediately for oversized WebSocket requests, openai/codex pull request 60840, 11 November 2026.
  5. Introducing advanced tool use on the Claude Developer Platform, Anthropic, 24 November 2025.
  6. This speed hack from OpenAI searches URLs instantaneously. and Too many MCP tools? Anthropic found a solution., Gaurav Sen, YouTube.
WhatsApp