Integration Layer or Model Output? Three Taps and Three Questions to Find Where a Wrong Answer Lives
Three receipts, three wrong totals, and everyone blames the model. A probe taps the request, the raw reply and the saved record, then asks three questions in order. Verdicts: one cut request, one parser that turned 18.50 into 185 cents, and only one real model mistake.
What you will learn
- Separate your integration layer (before and after the call) from the model output in the middle
- Tap the boundary: keep the request, the raw reply and its request id, and the saved record
- Ask three questions in order and let the first failure name the side
- Pick the fix that belongs to that side: your code, the prompt and evals, or the parser
Before you start
- The Claude Messages API: your first call
- Debugging a Claude app: error types and traces
A small app reads receipts and saves the total. One day a total comes out wrong, and somebody says: the model got it wrong again.
Maybe. But a wrong answer always has a home, and the model is only one of three places it can live. In this article we find which side the fault is on before fixing anything.
Where the fault can live
Think of a restaurant where you get the wrong dish. Maybe the waiter wrote half your order on the slip (the request). Maybe the kitchen made the wrong dish, and you can see it at the pass (the raw reply). Or the dish was right but went to the wrong seat (the record). You look at the slip, then the pass, then the table. You don't blame the kitchen before you read the slip.
Three reports, one blame
- Receipt R-202: the total is empty.
- Receipt R-203: the total shows 185 cents.
- Receipt R-204: the total says 40, the receipt says 44.
Same symptom, a wrong total. Three different places it can start.
Ask in order
- Did the whole receipt go out? If not, the fault is in the integration layer, request side.
- Is the answer in the raw reply right? If not, it's the model output.
- Did the record match the raw reply? If not, it's the integration layer again, response side.
The order matters, and the first failure names the side. A wrong request makes a wrong answer, and that answer isn't the model's fault.
receipt_app.py: our code on both sides
"""The integration layer: our code on both sides of the model call."""
import json
from stand_in import MODEL
MAX_CHARS = 60
PREFIX = "test model reply: "
ASK = "Read the receipt. Reply with JSON: merchant and total."
def build_request(case):
text = case["text"][:MAX_CHARS]
return {"model": MODEL, "max_tokens": 200, "system": ASK,
"messages": [{"role": "user", "content": text}]}
def parse_reply(message):
text = message.content[0].text.removeprefix(PREFIX)
fields = json.loads(text)
cents = None
if fields["total"] is not None:
tidy = fields["total"].rstrip("0")
cents = int(tidy.replace(".", ""))
return {"merchant": fields["merchant"],
"total_cents": cents}
Two bugs are already hiding here. Can you spot them before the probe does?
probe.py: tap, then ask
class Tap:
def send(self, client, request):
self.request = request
message = client.messages.create(**request)
self.raw = message.model_dump()
self.request_id = message._request_id
return message
def diagnose(case, client):
tap = Tap()
message = tap.send(client, build_request(case))
record = parse_reply(message)
sent = tap.request["messages"][-1]["content"]
reply = tap.raw["content"][0]["text"]
fields = json.loads(reply.removeprefix(PREFIX))
total = fields["total"]
want = None if total is None else round(float(total) * 100)
ok1 = sent == case["text"]
ok2 = fields == case["expect"]
ok3 = record["total_cents"] == want
...
verdict = "no fault"
if not ok1:
verdict = "integration layer, request side"
elif not ok2:
verdict = "model output"
elif not ok3:
verdict = "integration layer, response side"
return steps, verdict, tap.request_id
The tap keeps a copy of the request, the raw reply and its request id. Every API response carries a request id, and the Python SDK exposes it as _request_id. Keep it with every tap so a support ticket or a log search can find the exact call.
One verdict per receipt
The lecture runs the probe against a stand-in for Claude (the real SDK over a mock transport, scripted replies, test request ids), so the outputs below show the method, not a live model.
R-201 req_test_R201
1 request ok sent 52 of 52 chars
2 raw ok total 8.64 (expected 8.64)
3 record ok 864 cents, raw says 864
-> no fault
R-202 req_test_R202
1 request FAIL sent 60 of 77 chars
2 raw skip not judged
3 record skip not judged
-> integration layer, request side
R-203 req_test_R203
1 request ok sent 43 of 43 chars
2 raw ok total 18.50 (expected 18.50)
3 record FAIL 185 cents, raw says 1850
-> integration layer, response side
R-204 req_test_R204
1 request ok sent 53 of 53 chars
2 raw FAIL total 40.00 (expected 44.00)
3 record skip not judged
-> model output
verdicts: 1 no fault | 1 request side | 1 response side | 1 model output
R-202: the request was cut
MAX_CHARS = 60 keeps the first 60 characters of a 77-character receipt. The last 17 never leave, and that's where the total was. The other checks are skipped: a model can't read what was never sent. Fix our code.
R-203: the record drifted
The whole receipt went out, and the raw reply says 18.50, exactly right. But rstrip("0") strips the trailing zero, so 18.50 becomes 18.5, then the dot goes and we save 185 cents. The model was right; our parser changed the number. Because we saved the raw reply, we can run it through the parser again and get the same wrong number: a perfect reproduction.
R-204: the model output
The request was whole, and the raw reply says 40.00 where the receipt says 44.00. This one really is the model's. The fix is in the prompt: say what "total" means, add an example, and add this receipt to your evals. Patching the parser would not help.
Common mistakes
- Rewriting the prompt first. That may "fix" the wrong side, and R-202 and R-203 stay broken.
- Retrying until it looks right. That hides a cut request or a bad parser.
- Switching to a bigger model. No model reads text that was never sent.
- Logging only the parsed record. Without the raw reply you can't tell the model's mistake from the parser's.
- Judging the model on a broken request. Once question one fails, stop and fix the request.
Quick recap
| First failing question | Side | Fix |
|---|---|---|
| 1. Whole input sent? | Integration layer, request side | Our request-building code |
| 2. Raw reply right? | Model output | The prompt, plus an eval case |
| 3. Record matches raw? | Integration layer, response side | Our parser |
Once you know the side, choose a fix that holds on a new input too: see fix the cause, not the symptom.
Try it yourself
Add a tap to one model call in your own app: save the exact request, the raw reply with its request id, and the record you store. Next time someone says "the model got it wrong", answer the three questions before anyone touches the prompt.
Related course on Udemy
Claude Certified Developer - Foundations (CCDV-F) Exam Prep
Build and ship Claude apps: the API, agents, tools and MCP, prompts, evals, security and model choice.View on Udemy ↗: Claude Certified Developer - Foundations (CCDV-F) Exam Prep (paid course, opens in a new tab)Related posts
The Claude Messages API: Your First Call, Field by Field
One request in, one Message out. The four fields of a Messages API call (model, max_tokens, system, messages), how to read content blocks, stop_reason and usage, and why every turn resends the history.
Claude Tool Use: How tool_use and tool_result Work in the Messages API
Follow one order question through Claude's tool round trip: define a tool, read the tool_use block, run your own function and send a matching tool_result back, including parallel calls and is_error.
Claude Client SDKs Wrap One REST API: Raw HTTP, Python, TypeScript and Async Calls Compared
Every Claude client SDK wraps the same REST request: a POST to /v1/messages with a JSON body. See it built by hand, through the Python and TypeScript SDKs, as typed errors, and as async calls that wait together.
Anthropic SDK Timeouts, Retries and Error Classes: Configure the Client Once, Then Give 429, 529, 400 and Timeouts Their Own Move
Configure the Anthropic SDK client (timeout, max_retries, with_options, async, pagination), see which errors it retries by itself, then repair a retry wrapper so a 429, a 529, a 400 and a timeout each get the move the docs support.