Use case
How to cap tokens per API call
One unbounded request can turn a cheap workflow into an expensive one.
Create a token-cap policy that names a model and a limit, as either max_input_tokens or max_output_tokens. A request for that model over the limit is refused before forwarding, unless the policy is set to warn, in which case the call goes through with a warning header.
How it works
The cap is part of the same pre-call policy check as model restrictions. Each policy names one model, matched exactly, by prefix or by substring, and is applied to the request's token parameters rather than to accumulated spend.
x-spendline-policy-warnings: [{"code":"output_tokens_exceeded","policy_name":"Output cap","limit":4000,"actual":8000}]What to know before relying on it
A cap applies only to the model it names; a large call on another model is not affected. In warn mode the warning is returned only in that response's x-spendline-policy-warnings header and is not stored, so treat it as a signal to the calling code, not a record you can review later.
Why it matters
A monthly budget protects against sustained overspend; it does not stop one oversized request. A per-model token cap targets that specific risk.
Frequently asked questions
Does this cap input tokens, output tokens, or both?
Each policy is one type, max_input_tokens or max_output_tokens. Create two policies to cap both.
What happens to a call that would exceed the cap?
In block mode it is refused before reaching the provider. In warn mode it goes through and the response carries the warning header.
See what this would look like in your product.
Start with a guided 30-day free pilot on live production traffic. The 30 days start when your traffic flows through Spendline, not at sign-up. We agree the scope, success criteria and paid terms before you start. There is no Spendline subscription fee during the pilot; your model-provider charges still apply.
Prefer email? fida@spendline.ai