Implement Reliable LLM Calls with Independent Retry Budgets
Company: Stripe
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Online Assessment
Implement `reliable_call` around `client.complete(messages)` and `validate_fn(response)`. Preserve the caller's original conversation, including nested objects. Retry `ClientError` under an API-error budget and validation failure under an independent validation-error budget.
On a client error, append `CLIENT_ERROR_MESSAGE` to the working conversation. On a validation failure, append the failed response as an assistant message, then a correcting user message. When validation raises `ValidationError(reason)`, the correction must be `That response was invalid: {reason}. Please try again with valid JSON.`
Return the first valid response. On exhausted retries, raise `LLMRetryError` carrying `last_response`, `attempts`, `all_responses`, and `failure_reason`. Attempts count every API call; all_responses contains every validation-failed response. A budget of zero permits one initial call and no retry of that failure kind.
### Constraints & Assumptions
Assume CLIENT_ERROR_MESSAGE is a supplied message object. For a validator returning False, use the explicit reason `validation returned false`. Catch only the declared error types; unrelated programming errors propagate.
### Clarifying Questions to Ask
- Is each budget a retry count rather than a total-attempt limit?
- What exact correction text should a False result use?
- Is a response already a string suitable for an assistant message, or does the adapter need serialization?
```hint Count retries by failure type
A client failure must not consume a validation retry, but both kinds increase the total number of API calls.
```
### What a Strong Answer Covers
- Deep-copy isolation of the input and copied error metadata.
- Independent counters with correct zero-budget behavior.
- Conversation updates, failed-response history, and terminal failure reasons.
### Follow-up Questions
- Where would bounded backoff and request deadlines fit without changing retry accounting?
- How would you avoid retrying nonretryable authentication failures if the client exposes finer error types?
Overview: Implement reliable LLM calls with deep-copied conversations, independent client and validation retry budgets, correction messages, and complete failure metadata.
Implement reliable_call around client.complete(messages) and validate_fn(response). Preserve the caller's original conversation, including nested objects. Retry ClientError under an API-error budget and validation failure under an independent validation-error budget.
On a client error, append CLIENT_ERROR_MESSAGE to the working conversation. On a validation failure, append the failed response as an assistant message, then a correcting user message. When validation raises ValidationError(reason), the correction must be That response was invalid: {reason}. Please try again with valid JSON.
Return the first valid response. On exhausted retries, raise LLMRetryError carrying last_response, attempts, all_responses, and failure_reason. Attempts count every API call; all_responses contains every validation-failed response. A budget of zero permits one initial call and no retry of that failure kind.
Constraints & Assumptions
Assume CLIENT_ERROR_MESSAGE is a supplied message object. For a validator returning False, use the explicit reason validation returned false. Catch only the declared error types; unrelated programming errors propagate.
Clarifying Questions to Ask Guidance
Is each budget a retry count rather than a total-attempt limit?
What exact correction text should a False result use?
Is a response already a string suitable for an assistant message, or does the adapter need serialization?
What a Strong Answer Covers Guidance
Deep-copy isolation of the input and copied error metadata.
Independent counters with correct zero-budget behavior.
Conversation updates, failed-response history, and terminal failure reasons.
Follow-up Questions Guidance
Where would bounded backoff and request deadlines fit without changing retry accounting?
How would you avoid retrying nonretryable authentication failures if the client exposes finer error types?