When an agent needs a paid API, the simplest thing is to put the key in the service's environment variables and add to the prompt how much it may spend. The key works, the model takes the budget into account, and the demo looks fine.
We don't do that. Keys and money are controlled by code outside the model: the model does not see secrets and does not decide whether the next paid call is allowed. Below is why this matters and how it is built.
Where keys leak
A key in the service environment seems safely hidden until a tool fails. The error text often contains headers or a URL with parameters. The tool returns it to the model, the model retells the error in its answer or in a note for the next agent, and the key ends up in the conversation history and in the logs. The more tools and agents you have, the more such paths there are.
So the tool settings in our platform store only references to keys. The keys themselves sit in the company's encrypted storage, and the MCP runtime that executes the tools inserts them right before the call. The graph, the model and the conversation history only ever see the reference. If a company connects its own MCP server, that server's key is stored the same way.
Similar logic applies to code that the model writes itself. If an agent analyses data in Python, that code runs in a WebAssembly sandbox: a separate process per task, with no network and no environment variables, and with limits on time and memory.
Why a budget in the prompt does not work
We tested this in practice. The budget for paid search was written into the agents' instructions, and the agents took it into account but did not always stick to it. Deep research, which launched several agents, had no overall limit at all.
A budget that has to hold must be checked by code before the call. In our research graph every paid call is written to a ledger, and a call over budget is rejected before it is made. The model gets a clear refusal and decides what to do next without that data.
Money is counted across the chain
When an agent calls other agents, the cost of one answer adds up across the whole chain. A limit per call does not protect against this: each call stays within its own limit, and together they go beyond any reasonable bounds.
So spending limits in the platform are set in dollars at three levels, per day and per month: per company, per agent and per end user. An agent's limit includes every agent it called, at any depth. The limit is checked before every answer. At 80% a warning goes out, and at 100% the agent stops answering until the next period.
To have something to count, all model calls go through one router. It gives access to OpenAI, Anthropic, Mistral, Groq and Together and knows which agent and which company each call belongs to. If a client has its own contract with a provider, the router runs on the client's key: tokens are counted, but the platform does not charge for the model. Every agent answer has a trace that shows which models and tools were called and what they cost.
What it costs us
A single entry point to models and tools adds a link that can slow down or break. At one point our model calls were being cut off at exactly the sixtieth second: a reasoning model thought for more than a minute, and the load balancer in front of the router closed the connection on its idle timeout. We had to move the traffic to the router inside the cluster.
The accounting is not perfect either. Prices in the model catalog differ from the provider's actual rates, so the platform's figures are closer to an estimate than to an invoice. And anything that bypasses the single entry point falls out of the accounting: for a while our LLM judges for quality checks worked that way.
The limit itself also requires decisions. If the limit check cannot run, we let the answer through: better to overpay a little than to leave a user without an answer because accounting failed. And when the limit is used up, the agent goes silent, and the user needs a clear explanation of why.
The opposite mistake
Limits that are too tight kill the agent's usefulness. Research that did not have the budget for three search queries returns an empty answer, and that is worse than overpaying a little. We set limits so that they catch accidents like an endless loop rather than normal work, and we look at the traces where an agent hit a limit.
What's next
In Flutch these mechanisms are shared by all agents on the platform. If you build agents yourself, three things are worth doing right away: store references to keys in the settings instead of the keys, check the budget in code before the call, and count money across the whole chain of agents rather than per call.