Idempotency of LLM API Requests: Keys, Deduplication, and Retries
Idempotency of LLM API requests prevents redundant generation and double token charges during retries, timeouts, and parallel requests from a single user.
News, analyses, and notes about AI.
Idempotency of LLM API requests prevents redundant generation and double token charges during retries, timeouts, and parallel requests from a single user.
The LLM structured output repair loop automatically fixes invalid JSON and schema errors by re-invoking the model with a description of the defect instead of failing the entire operation.
Batch API is a mode where requests to the model are sent not individually but in batches as a job file. The provider processes the batch asynchronously over hours and returns the result in a separate file. Compared to synchronous mode, the discount is usually 40–50%, but the cost is a delay: the response comes not in seconds but in minutes or hours.
The cost of a request and the likelihood of a context_length_exceeded error are determined by the number of tokens in the input and output. Estimating them by eye based on characters or words is inaccurate, with discrepancies reaching double. Precise counting before sending is a mandatory step in any production pipeline.
When a single LLM API serves multiple tenants—clients, teams, or projects—risks arise: data leakage between tenants, resource exhaustion by one consumer, context contamination in prompts, and exposure of system instructions. Isolation addresses these issues at the level of keys, quotas, storage, and prompts. Below is a practical schema that I apply in production.
Caching prompts reduces the cost and latency of LLM API, but it introduces the risk of serving outdated responses. The issue arises when the system prompt, model version, knowledge base content, or business logic rules change, yet the cache continues to return results based on the previous configuration. In this article, I discuss a practical scheme for invalidation: how to build cache keys, what fields to include in the version, how to tag records, and how to organize cascading resets.
The context window is the most expensive resource in an LLM API. When it overflows, the model starts to 'forget' the beginning of the dialogue, the quality of responses drops, and costs rise. Below is how to build a context window monitoring system that warns about problems before they become critical.
JSON mode is a convenient way to make a model return structured data. In practice, it solves some problems but creates new ones. Below are the typical pitfalls that arise when integrating JSON mode in production, and how to work around them.

Costs for LLM API can grow unpredictably, especially when scaling. Below is how to estimate costs based on tokens, build growth models, and use tools for budget control.

Security often takes a backseat when working with LLM APIs: first, the model needs to respond, then faster and cheaper. But when the system goes to production, security questions become critical. Below are the main threats and practical security measures for LLM APIs: from key storage to protection against prompt injection and data leaks.