APIs
Intermediate 13 min read APIs

Debugging API Errors

Read the wire before rebuilding the world

Most model-integration failures are diagnosable from the HTTP exchange: status code, response body, headers and timing. This is a methodical playbook for reading those signals.

debuggingstatus codesrequest idtracingerror bodiestimeouts
Before we start

What you will learn

  • Diagnose failures from status code plus response body
  • Use request IDs and timing to localize the fault
  • Reproduce API issues with minimal curl probes
Introduction

What is this?

Debugging an AI API integration is reading four signals in order: the status code (what class of failure), the response body (what's wrong specifically), the headers (rate limits, request IDs, retry hints), and the timing (where latency is lost). Most 'the model is broken' reports are actually config, auth, proxy or retry bugs visible at the wire level.

Motivation

Why do you need it?

  • Status + body identifies 80% of issues before any code is touched.
  • Request IDs let you correlate a failing call across provider, gateway and your logs.
  • Timing breakdown (connect vs TTFT vs completion) shows whether the problem is your network, the proxy or the model.
  • Curl reproductions strip away SDK and framework noise to expose the real exchange.
Think about it

Mental model

Imagine

A doctor's vital signs

Think of

Status, body, headers, timing

You check vitals first — temperature, pulse, color — before guessing at diseases. API debugging is the same: quick observable signals before deep investigation.

Mechanics

How it works

  • Category the code: 4xx = your request is wrong; 429 = pressure; 5xx = their side; timeout = handled on your side.
  • Read the body; providers write field-level validation errors you can act on directly.
  • Grab request IDs from headers; they name the exact transaction across systems.
  • Time the phases: DNS+TCP+TLS, first byte, then completion.
  • Reduce: strip streaming, reduce a huge prompt to a minimal repro, then grow it back.

The debugging funnel.

Status code
Response body
Headers
Timing
Minimal repro
Real world

In production

  • On-call sees a spike of 400s; error bodies reveal a schema change, and a client cache is refreshed without provider involvement.
  • A 500 burst from one provider is correlated via request IDs to a gateway region, not the codebase.
  • A latency regression breaks down as connect+TTFT growing — pointing at the gateway pool, not the model.
Guidelines

Best practices

  • Log status, request id, latency and a redacted body summary for every model call.
  • Keep a curl repro template per provider for fast triage.
  • Correlate failures by request ID across layers (handler → gateway → provider).
  • Alert on rate and type changes, not just raw failure count.
Watch out

Common mistakes

  • Debugging as if every failure is a code bug when most are config/auth/limits.
  • Not logging the response body, so every issue needs reproduction from scratch.
  • Treating a 500 from a gateway the same as from a provider — different owners.
  • Chasing a timeout without checking whether a proxy silently dead-connected.
Root cause analysis

Common issues

Error
400s pointing at a field your code doesn't touch
Root cause

An SDK upgrade changed serialization (e.g. nulls or enum spelling)

Fix

Inspect the raw JSON the SDK emitted and align with the provider schema.

Error
Timeout at 110s that always happens mid-stream
Root cause

Proxy/provider idle timeout firing on slow tokens

Fix

Timeout only the connection phase; let active streams run, or tune the proxy idle timeout.

Error
Intermittent 500s from one payload only
Root cause

A content-specific failure server-side (e.g. huge context)

Fix

Repro with curl, reduce context, and report with the request ID.

When things break

Debugging tips

  • Start with curl and the smallest body; add fields back until it breaks.
  • Compare a working vs failing request line by line (headers, body, URL).
  • Check the clock: TLS and cert errors are often time-skew.
  • If the SDK swallows errors, call the transport directly for one raw response.
Interviews

Interview checkpoint

Test yourself

Quick quiz

0/2
1

Which signal identifies the exact transaction across provider and your logs?

2

A 400 error always indicates a bug on the provider's side.

Go deeper

Official documentation