BNK
Inside the LLM gateway
An agent's calls, byte by byte
An agent is a program that calls a model in a loop. It sends a task, the model asks for a tool, the agent runs the tool and sends the result back, and so on until the job is done. One task can be ten calls or a thousand, and nobody is watching each one. The LLM gateway is what stands between that loop and the models. This lesson follows one agent through three turns and opens every message. You will see the gateway swap the agent's key for a real one, hold back budget before each call, send one turn to a GPU in this factory and the next to another company's model, rewrite the request into that provider's format and the answer back again, survive the provider failing, and finally stop the agent when its budget is gone. In this factory the gateway is F5 AI Gateway. The same jobs can also run in the BNK data plane as policy, which the guardrails lesson shows.
- Caller
- an AI agent with one tool
- Speaks
- the OpenAI chat format, always
- Models behind it
- one on our GPUs, one hosted elsewhere
- The agent holds
- a virtual key, never a provider's key
Read the whole lesson as text
1. The agent's first turn
The agent has been asked to find out why a customer was charged twice. It sends its first request to the factory. It looks like a chat request with one addition: a list of tools. Each tool is a name, a description and the shape of its arguments. The model can't run a tool. It can only answer with a request to run one, and the agent does the running. The agent identifies itself with a key that was issued for this agent alone.
- The agent sends its task, with the list of tools the model may ask for.
- TMM ends the TLS session and passes the request to the gateway.
2. Key, scope, budget
The gateway does three things before it calls anyone. It looks up the key. The key is a virtual key: a name the gateway invented, which stands for this agent and is worth nothing anywhere else. Behind it is a small tree: an organization, a team, a user and the key, each with its own budget. Then it checks the scope: is this key allowed to use the model it asked for. Then comes the budget, and here the gateway is careful. It doesn't know yet what the call will cost, so it holds back the most it could cost: the whole prompt plus max_tokens. If that doesn't fit, the call is refused now, before the model spends anything.
- The gateway looks up the key and finds the agent, its team and its organization.
- It checks that this key may use the model it asked for.
- It counts the prompt, adds the most the answer could be, and holds that much of the budget back.
3. A model on our own gpus
The name northlight-chat is an alias. The gateway's catalog says it means qwen3-32b, and that this model is served here, behind BNK. So the gateway treats the factory's own inference listener as one more provider. It rewrites the model name, drops the agent's key, and sends the request to BNK. TMM asks the endpoint picker and forwards the request to gpu-2. Nothing has left the building, and the agent's key never reached a model server.
- The alias points at a model served in this factory, so the gateway sends the request to BNK.
- TMM asks the endpoint picker, which chooses gpu-2, and forwards the request there.
4. The model asks for a tool
The model reads the task and decides it needs the invoices. It doesn't write text. It writes a tool call: the name of the tool and the arguments, as JSON. In the stream this arrives as chunks whose delta holds tool_calls and not content, with the arguments built up piece by piece. The last of them carries finish_reason tool_calls, one more chunk carries the token counts, and the stream closes with [DONE]. The gateway passes it through unchanged, because the agent already speaks this format. Then the gateway settles the bill for this turn. It held back 2,652 tokens. The call used 2,178. The difference goes back into the budget.
- The model answers with a request to run a tool: its name and its arguments.
- The gateway reads the real token counts and gives back what it held in reserve and didn't need.
- The agent receives the tool call. Running the tool is the agent's job, not the model's and not the gateway's.
5. The second turn is bigger
The agent has run the tool. Now it calls again, and it has to send the whole story: its instructions, the task, the model's tool call and the tool's result. The API keeps no memory between calls, so each turn of the loop is longer than the last. The gateway holds back budget again, for a bigger prompt this time. But look at what the GPU sees. Almost all of the first 2,178 tokens are what gpu-2 read a moment ago. The picker sends the turn to gpu-2 again, and prefill only has to read the new part. An agent loop is the best case for cache-aware routing.
- The agent calls again and resends everything, with the tool's result added at the end.
- The picker sends this turn to gpu-2 again, where most of the prompt is already in the KV cache.
6. A harder question, another company's model
The invoices show two charges for one plan, and the agent wants a stronger model to reason about refunds. For its third turn it asks for northlight-frontier. That alias points outside the factory, at a model hosted by another company. That company's API is not the OpenAI format. It wants the instructions in a field of their own, tool calls as blocks inside a message, and headers of its own, a version header among them. The agent knows none of this and doesn't change a line. The gateway rewrites the request. It also swaps keys: the agent's virtual key stays in the gateway, and the provider's real key, which only the gateway holds, goes out in its place.
- For the next turn the agent asks for a stronger model, by a different alias.
- The alias points at a hosted model with a different API, so the gateway rewrites the request into that format.
- The translated request leaves the factory for the provider, over TLS, with the provider's own key.
7. Translate the stream back
The provider streams its answer in its own event format: named events, text arriving in content_block_delta events, token counts split between the first event and a message_delta event near the end. The agent expects chat.completion.chunk lines ending in [DONE]. So the gateway translates in the other direction too, event by event, as they arrive. It can't wait for the whole answer, because the agent is reading a stream. At the end it has the provider's token counts, turns them into a price from its price list, and settles the budget in tokens and in money.
- The provider streams its answer as named events in its own format.
- The gateway rewrites each event into the chunk format the agent expects, as it arrives.
- The agent receives an ordinary chunk and can't tell that the model was somewhere else.
- At the end of the stream the gateway adds up the provider's token counts and puts a price on the turn.
8. The provider fails, the agent doesn't notice
The agent's fourth turn goes to the same hosted model, and this time the provider is overloaded and refuses. In the gateway's catalog the alias has more than one deployment: the same model reached a second way, with a lower priority. The gateway doesn't pass the error on. It sends the same request to the second deployment, in whatever format that one needs. The agent sees one request and one answer that took a little longer. Nobody had to teach the agent about retries, regions or a second set of keys.
- The agent's next turn goes to the hosted model's first deployment.
- The provider answers that it is overloaded and refuses the request.
- The gateway keeps the error to itself and sends the same request to the second deployment.
9. The loop runs out of budget
Every call ends with a record: which key, which user and team, which model and deployment, how many tokens, what it cost. The gateway sends these to the usage service and ships its logs to wherever the operator keeps them. And the records add up. The agent keeps looping, and on its ninth turn the amount to hold back no longer fits in the key's daily budget. The gateway refuses with 429 before calling any model. This is the guard that matters most for agents. A person stops asking at some point. A loop with a bug does not, and without a budget it runs until someone reads the bill.
- After each call the gateway writes a record of who called what and what it cost.
- The agent is still looping. Its ninth turn carries a prompt twice the size of the first.
- What the gateway would have to hold back no longer fits in the key's daily budget, so it refuses.
Summary
The agent spoke one format to one address with one key, and behind that the gateway did everything else. It turned a virtual key into an identity with budgets at four levels. It held budget back before each call and settled it after. It sent one alias to a GPU in this factory, where the growing conversation kept hitting the KV cache, and another alias to a hosted model, translating the request and the stream in both directions and using a key the agent never saw. It hid a provider failure behind a second deployment. And it stopped the loop when the budget ran out. For a person these are conveniences. For an agent, which never gets tired and never reads the bill, they are the controls.
Next lesson: Guardrails: in the path or beside it