rohitg00/ai-engineering-from-scratch · error · RuntimeError
Messages API returned HTTP
Error message
Messages API returned HTTP {error.code} What it means
Raised in AnthropicMessagesHTTPTransport.create_message when urllib raises an HTTPError and the error code is re-raised with Messages API context. It marks an HTTP-level failure (4xx/5xx from the endpoint), distinct from transport timeouts or malformed response bodies.
Solutions
- Check error.code: 401/403 mean bad credentials, 429 means rate limited, 5xx are server-side
- Retry with backoff on 429 and 5xx responses
- Verify the endpoint URL, x-api-key header, and anthropic-version are correct for 4xx errors
Defensive patterns
Strategy: retry
When it happens
Trigger: Thrown at certifications/claude/lessons/30-developer-application-capstone/code/main.py:66 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/a05301ec74addc9d.
Report an issue: GitHub.
Appendix: source
Thrown at certifications/claude/lessons/30-developer-application-capstone/code/main.py:66
self._opener = opener
def create_message(self, payload: dict[str, Any]) -> dict[str, Any]:
body = json.dumps(payload).encode("utf-8")
request = urllib.request.Request(
self.endpoint,
data=body,
method="POST",
headers={
"content-type": "application/json",
"anthropic-version": ANTHROPIC_VERSION,
"x-api-key": self._api_key,
},
)
try:
with self._opener(request, timeout=self.timeout_seconds) as response:
decoded = json.loads(response.read().decode("utf-8"))
except urllib.error.HTTPError as error:
raise RuntimeError(f"Messages API returned HTTP {error.code}") from None
if not isinstance(decoded, dict):
raise ValueError("Messages API response must be an object")
return decoded
def build_live_order_request(request: str, model: str) -> dict[str, Any]:
"""Build an inspectable wire payload with the same bounded lookup contract."""
return {
"model": model,
"max_tokens": 128,
"system": (
"You are a bounded order-status router. Use lookup_order only when "
"the user supplies an exact public order ID. Never claim execution."
),
"messages": [{"role": "user", "content": request}],
"tools": [
{
"name": "lookup_order",View on GitHub (pinned to 39ea8a1c6d)