How an endpoint is chosen
TokenChange considers the model’s endpoints that are serving, that your key’s scope allows and that fit your price options. Among those:- Endpoints that are up come first. Degraded endpoints are only used when no endpoint that’s up can take the request.
- Traffic is shared by capacity. An endpoint with more slots receives a proportionally larger share of requests.
- Similar requests stick together. Requests with the same prompt prefix go to the same endpoint, so the provider’s prompt cache is reused. See Prompt caching.
- Busy endpoints are skipped. If an endpoint is near capacity, the request goes to the next choice instead of queueing.
Failover
If an endpoint fails before it sends you any output, TokenChange retries the request on another endpoint. You only see the stream from the endpoint that succeeds, and the failed attempt isn’t billed.- A request is tried on up to three endpoints, and no new attempt starts more than 30 seconds after the request arrived.
- Once output has started streaming, the request stays on that endpoint. If the stream breaks after that, it isn’t retried.
- Failover stays within your key’s scope and your price options.
- Pinned requests never fail over.
Prompt caching
Providers cache the processed start of a prompt. When the next request starts the same way, the cached part is faster to process and billed at the endpoint’s cached input price. By default, TokenChange groups requests by the start of the prompt:- For chat completions, the tools, the system and developer messages and the first user message.
- For text completions, the start of the prompt.
temperature and max_tokens don’t affect this.
To control grouping yourself, set prompt_cache_key. Requests with the same key go to the same endpoint while it has capacity.
Price options
Add aprovider object to a request to filter or order endpoints by price. These options use the same shape as OpenRouter’s provider field.
number | string
Highest input price you accept, in USD per million tokens. Endpoints priced above it are skipped.
number | string
Highest output price you accept, in USD per million tokens. Endpoints priced above it are skipped.
string
Set to
"price" to try the cheapest endpoints first. Endpoints are ordered by input plus output price, and requests are shared among endpoints at the same price. No other value is accepted.- If no endpoint fits within
max_price, the request fails with400. - Failover never moves a request above your ceiling.
- You’re always billed at the prices of the endpoint that served the request.
Use price options with an OpenAI SDK
OpenAI SDKs don’t know theprovider field. Pass it as an extra body parameter: