# Long context and concurrency

> Budget prompt and output tokens, stream lengthy requests, and handle load and retries.

Source: https://i14.ai/docs/api-usage/long-context/

Start with [Chat Completions](https://i14.ai/docs/api-reference/chat-completions/) for long-context generation. Some long-context deployments accept only Chat Completions and [Completions](https://i14.ai/docs/api-reference/completions/). A `400` indicating that the model is served as a read/write split means you should switch endpoints, not retry unchanged.

## Keep the request within two limits

1. **Context:** Use [List models](https://i14.ai/docs/api-reference/models/) to check `max_model_len` when it is reported. Budget for the entire input, including history, instructions, tool definitions and the model’s chat template, plus the maximum output. Leave headroom for reasoning when enabled. A previous answer’s `usage.prompt_tokens` helps estimate a subsequent turn with the same history.
2. **Bytes:** The API can return `413` if the serialized request is too large. Token count and request size are different. For documents, extract the relevant text and serialize it as JSON. Avoid attaching a whole file or adding base64 data when plain text will do. The API does not fetch remote URLs.

For long requests, let a JSON library escape the document. For example, use `json.dumps` in Python, `JSON.stringify` in JavaScript, or `jq --rawfile` in shell. Avoid interpolating a document directly into a handwritten JSON string.

## Stream long requests

The server reads the prompt before the first generated text. Long uploads, prompt processing, model startup and queueing all add to that wait. Stream with `stream: true` so output can arrive as it is generated. Keepalive comments can appear before the first token; they are not a promise of time to first token or a maximum pause. Keep separate connection, read and application deadlines suited to your workload, and handle both HTTP errors and [errors inside the stream](https://i14.ai/docs/api-usage/streaming/#handle-keepalives-and-interruptions).

For Chat Completions set `stream_options.include_usage: true` to get token totals at the end when available.

## Reuse the prefix across turns

On deployments that support session affinity, send the same `X-Session-ID` for turns in one conversation to improve the chance of reusing cached prompt work. The ID is an optional, non-sensitive string of at most 256 characters. Use a different ID for unrelated conversations. This does not store conversation state: continue to send the history. Cache reuse is best effort and can be lost after changes in routing, restarts, or eviction.

Keep the prefix stable when possible. Append messages and tool results rather than changing earlier messages or repeatedly inserting a timestamp into the system prompt. Changing earlier input can reduce prefix reuse. Remove irrelevant material when you need to stay within context; correctness matters more than cache reuse.

## Bound concurrency and retries

* Reuse one SDK client per process so requests can reuse connections.
* Cap in-flight work with a semaphore or worker pool. Start below your expected load and tune using observed first-token latency, throughput and errors. Capacity varies across deployments.
* Retry transient `429`, `502`, `503` or `504` responses with a bounded number of attempts and exponential backoff with jitter. Honor `Retry-After` when it is present. If the same long non-streaming request repeatedly times out, switch to streaming or reduce its size rather than repeatedly resending it.
* Do not retry unchanged `400`, `401`, `403` or `413` requests. Fix the input, credentials, access or size first.
* If the stream fails after output starts, treat any later attempt as a new generation. Do not mix streamed answers or repeat independently executed tool actions.

When reporting a problem to your i14 contact, include the model ID, endpoint, approximate UTC time, HTTP status, request ID or a redacted error where available, approximate prompt size and your non-sensitive session ID. Do not send an API key or the prompt itself.
