A production guide to handling rate limits, context overflow, and probabilistic failure.
If you are coming from traditional software engineering, working with Large Language Models (LLMs) requires a fundamental shift in mindset. In traditional dev, APIs are deterministic: if you send valid JSON to a payment gateway, it succeeds 99.99% of the time, and errors are usually signifiers of a bug.
LLM APIs are different. They are probabilistic and resource-constrained by design.
The most common error you will encounter is 429 Too Many Requests.
Providers enforce limits on:
Common causes include traffic bursts, aggressive retry logic, or parallel agents spinning up too many threads at once.
Naive code retries immediately upon failure.
This creates a "Retry Storm" that hammers the API provider, guaranteeing you get blocked for longer.
The standard solution is Exponential Backoff: wait a short time, then double the wait time for each subsequent failure (1s, 2s, 4s, 8s). Crucially, you must add Jitter (randomness). If 1,000 users all fail at 12:00:00 and all retry exactly at 12:00:01, they will just DDOS the server again. Jitter spreads out the retries.
import time
import random
def call_llm_with_backoff(prompt, max_retries=5):
for attempt in range(max_retries):
try:
return api.generate(prompt)
except RateLimitError:
# Exponential Backoff: 2^attempt
# Jitter: random float between 0 and 1
delay = (2 ** attempt) + random.uniform(0, 1)
print(f"Rate limited. Retrying in {delay:.2f}s...")
time.sleep(delay)
raise Exception("Max retries exceeded")
Every model has a strict Context Window—the maximum amount of text (tokens) it can hold in its "working memory" at once.
This is a hard limit. You cannot "buy" more context for a specific request. If you send 129k tokens to a 128k model, the API will throw a 400 Bad Request or simply truncate (cut off) the end of your text silently.
When you have a 500-page PDF and a 10-page context limit, you need architectural patterns to handle the overflow.
Break the document into smaller pieces (chunks) with overlap.
Pros: Preserves local detail. Great for "Find X in this document" tasks. Cons: Loses global context. Chunk 3 doesn't know what happened in Chunk 1.
This preserves global context by maintaining a running summary.
Pros: Excellent for coherency and "big picture" understanding. Cons: Slow (serial processing). Errors in the early summary accumulate and poison the final result.
In Agentic workflows, LLMs often feed into each other.
If Agent A creates a subtle bug, Agent B might hallucinate that it's "correct," and Agent C crashes production. This is Failure Propagation.
Key Insight: Confidence ≠ Correctness. An LLM will often be most confident when it is hallucinating. You cannot rely on the model to "self-correct" reliably without external validation tools (like running the code, checking syntax, or verifying against a database).
Since you cannot eliminate errors, you must design for them.
Debugging LLMs is notoriously hard because the "bug" might be a nondeterministic roll of the dice. To survive in production, you need aggressive Observability.
You must log:
LLMs are powerful, but they are bounded systems. They are constrained by math, memory, and network physics. Reliable applications emerge when those bounds are designed for—not ignored.