Which Errors to Retry
Retry these status codes
Do NOT retry these status codes
Fix the underlying cause before attempting another request.Special case: context_length_exceeded
You do not need to handle this one manually. When a context_length_exceeded error occurs and your request includes a thread_id (or conversation_id), the API automatically retries once after compacting older messages into a structured summary. The retry is transparent — you receive a successful response as if the error never happened. Without a thread_id, reduce your message history and retry yourself.
Exponential Backoff Strategy
Use the following parameters for all retryable errors:- Base delay: 1 second
- Maximum delay: 8 seconds
- Jitter: add a random fraction of the current delay to avoid thundering-herd collisions
Retry-Afteroverride: on429responses, use the value from theRetry-Afterheader as the minimum wait, then apply your backoff on top for subsequent attempts
Retry-After:
If you prefer a library approach, tenacity integrates cleanly. Wrap the
httpx.post call with @retry(wait=wait_exponential(min=1, max=8), stop=stop_after_attempt(5)) and add a before_sleep hook to inspect Retry-After.Request Tracing
Attach a uniqueX-Request-ID header to every request. Piramyd echoes the value back in the response headers, and streaming errors include it in the request_id field of the error payload. Persisting this ID in your logs dramatically reduces time-to-resolution when you open a support ticket.
Upstream Errors
A502 response means an upstream inference provider returned an error or became temporarily unreachable. Piramyd normalises all upstream errors into the same standard error shape before returning them to you — you do not need to handle provider-specific error formats.
Apply the same exponential backoff logic you use for other 5xx responses. Upstream outages are typically short-lived, and a request that fails immediately often succeeds within a few seconds.
