Reconstructing a Bad Response: What You Need Logged to Debug Later
Debugging a poor model response after the fact is nearly impossible without capturing the right data upfront. This article explains what to log—prompt, parameters, output, latency, and context—so you can reconstruct and diagnose issues, and how to do this while working with multiple models through a unified API.
Why Reconstructing a Bad Response Is Hard
By the time you notice a bad response, the original interaction may be gone. Without a record of the exact prompt, parameters, and output, you cannot:
- Reproduce the issue to confirm it recurs.
- Isolate whether the model, the prompt, or a parameter caused the problem.
- Compare behavior across different models to find the best fit.
This is especially true when you use multiple models from different providers. Each provider may log differently—or not at all. A unified API gateway can help by giving you a single place to capture the data you need.
What to Log
At minimum, capture the following for every request. Store them together so you can replay the scenario later.
The Prompt
- Full text of the prompt after any templating or variable substitution.
- System message if separate.
- Any few-shot examples included.
- Tools or function schemas if used.
Without the exact prompt, you cannot know what the model actually saw.
Model and Parameters
- Model identifier (e.g., the exact model name you called).
- Temperature, topp, topk, max_tokens and any other sampling controls.
- Stop sequences or other constraints.
- Presence and frequency penalties if applicable.
Different parameter values can drastically change output. If you don't log them, you can't tell whether a tweak caused the bad response.
The Output
- Raw response as returned by the API, including any metadata.
- Finish reason (e.g., why the model stopped generating).
- Token usage (prompt and completion tokens) to track cost and spot truncation.
If the model returned multiple choices or tool calls, log those too.
Timing and Environment
- Timestamp with timezone.
- Latency (time to first token and total duration) to detect performance regressions.
- Request ID or correlation ID from the provider or gateway, if available.
- User or session identifier (anonymized if needed) to group related interactions.
Context and State
- Conversation history if the request is part of a multi-turn chat.
- Any retrieved documents or external data injected into the prompt.
- Feature flags or app version that might affect how the prompt was constructed.
How to Implement Logging
Centralize Through a Gateway
If you call multiple providers directly, you need to integrate logging separately for each. A unified API aggregator can simplify this: you send requests to one endpoint, and you can log in a consistent format regardless of the underlying model. This also makes it easier to switch models without changing your logging code.
Store Logs with Enough Retention
Bad responses may be noticed days later. Keep logs long enough to allow investigation. Consider storing them in a structured format (e.g., JSON) in a database or log service that supports querying.
Avoid Logging Sensitive Data Carelessly
Prompts and outputs may contain user data. Anonymize where needed and follow your privacy policies. If you need to debug with real data, ensure access is restricted.
Using Logs to Debug
Once you have the data, you can:
- Reproduce the issue by replaying the exact request.
- Isolate variables by changing one parameter at a time.
- Compare models by running the same prompt against alternatives.
- Identify patterns by grouping similar failures.
Without logs, you are guessing. With them, debugging becomes a systematic process.
Practical Tips
- Log at the point of API call, not just at the application layer, to capture the final prompt sent.
- Include a unique request ID and propagate it through your system.
- If using a gateway, check whether it provides request logs or a way to export them.
- Review logs periodically to catch issues before they become widespread.
- When you find a fix, update your logging to capture the new relevant fields.
Conclusion
Reconstructing a bad response requires capturing the right data at the right time. By logging the prompt, parameters, output, timing, and context, you build a foundation for effective debugging. Using a unified API gateway can make this consistent across models, so you spend less time reconstructing and more time fixing.