惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
MongoDB | Blog
MongoDB | Blog
D
Docker
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
Jina AI
Jina AI
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
B
Blog RSS Feed
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
Vercel News
Vercel News
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
I
InfoQ
U
Unit 42

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Normalize Provider Error JSON So Your Agent Can Actually ...
Mukunda Rao · 2026-05-26 · via DEV Community

Anthropic's rate limit error looks like this:

{"error": {"type": "rate_limit_error", "message": "Rate limit exceeded"}}

Enter fullscreen mode Exit fullscreen mode

OpenAI's rate limit error looks like this:

{"error": {"code": "rate_limit_exceeded", "message": "...", "param": null, "type": "tokens"}}

Enter fullscreen mode Exit fullscreen mode

Google's looks like this:

{"error": {"code": 429, "message": "Quota exceeded", "status": "RESOURCE_EXHAUSTED"}}

Enter fullscreen mode Exit fullscreen mode

If your agent handles multiple providers, you end up with provider-specific error parsing code scattered everywhere. llm-pretty-error normalizes them into a single LLMError object.


The Shape of the Fix

from llm_pretty_error import normalize_error, LLMError

try:
    response = call_provider(prompt)
except Exception as raw_error:
    error: LLMError = normalize_error(raw_error)

    print(f"Provider: {error.provider}")     # "anthropic"
    print(f"Kind: {error.kind}")             # "rate_limit"
    print(f"Message: {error.message}")       # human-readable
    print(f"Retry after: {error.retry_after_seconds}")  # int or None
    print(f"Status code: {error.status_code}")  # 429
    print(f"Request id: {error.request_id}")  # provider request ID for support

Enter fullscreen mode Exit fullscreen mode

One object. Same fields regardless of which provider threw the error.


What It Does NOT Do

llm-pretty-error does not retry for you. Normalization and retry are separate concerns. Use llm-retry-py or llm-fallback-chain for retry logic.

It does not cover every possible provider error. It covers the most common ones (rate limit, auth, not found, context length, content filter) from Anthropic, OpenAI, and Google. Novel error types from less common providers fall through to kind: "unknown".

It does not modify the original exception. normalize_error() returns a new LLMError dataclass. The original exception is attached as error.original_exception for full stack trace access.


Inside the Library

Detection is provider-first, then error type:

def normalize_error(exc: Exception) -> LLMError:
    provider = detect_provider(exc)
    status_code = extract_status_code(exc)
    raw_body = extract_response_body(exc)

    if provider == "anthropic":
        return parse_anthropic_error(exc, status_code, raw_body)
    elif provider == "openai":
        return parse_openai_error(exc, status_code, raw_body)
    elif provider == "google":
        return parse_google_error(exc, status_code, raw_body)
    else:
        return LLMError(
            provider="unknown",
            kind="unknown",
            status_code=status_code,
            message=str(exc),
            original_exception=exc,
        )

Enter fullscreen mode Exit fullscreen mode

Provider detection: Anthropic SDK exceptions have type(exc).__module__.startswith("anthropic"). OpenAI SDK exceptions have type(exc).__module__.startswith("openai"). Google SDK exceptions have class names starting with known Google error prefixes.

request_id: Anthropic includes a request-id header in error responses. OpenAI includes x-request-id. These are invaluable for filing support tickets. normalize_error() extracts them if available.

The 31 tests cover: each provider's major error types (rate limit, auth, not found, context length, content filter, server error), unknown provider fallthrough, missing status code, missing request ID, and the original_exception attachment.


When to Use It

Use it in any multi-provider agent where you want unified error handling. The normalization adds a small overhead (a few microseconds) per error. For error paths, that is negligible.

The request_id field is worth its weight in gold when you have a provider error that needs support investigation. Include it in your error logs:

logger.error(
    "llm_call_failed",
    provider=error.provider,
    kind=error.kind,
    status_code=error.status_code,
    request_id=error.request_id,
    message=error.message,
)

Enter fullscreen mode Exit fullscreen mode

When you file a support ticket with Anthropic or OpenAI, the request_id is the first thing they will ask for.


Install

pip install git+https://github.com/MukundaKatta/llm-pretty-error

Enter fullscreen mode Exit fullscreen mode

from llm_pretty_error import normalize_error, LLMError, ErrorKindLike
from llm_retry_py import LLMRetry

retry = LLMRetry(max_attempts=3, base_delay=1.0)

def call_with_normalized_errors(prompt: str) -> str:
    try:
        return retry.call(
            fn=lambda: call_llm(prompt),
            classify_error=lambda e: normalize_error(e).kind,
            retryable_kinds={"rate_limit", "server_error"},
        )
    except Exception as e:
        error = normalize_error(e)
        if error.kind == "context_length":
            # Trim and retry
            return call_with_shorter_prompt(prompt)
        elif error.kind == "content_filter":
            return "I cannot help with that request."
        else:
            logger.error("unhandled_llm_error",
                        kind=error.kind,
                        provider=error.provider,
                        request_id=error.request_id)
            raise

Enter fullscreen mode Exit fullscreen mode


Sibling Libraries

Library What it solves
tool-error-classify Classify tool call exceptions by ErrorKind enum
llm-retry-py Retry logic using error classification
llm-circuit-breaker-py Open circuit after repeated failures
llm-fallback-chain Route to backup provider on failure
agent-run-id Thread run IDs through logs for correlation

llm-pretty-error + tool-error-classify cover both sides of agent error handling: LLM API errors and tool execution errors, both normalized to comparable representations.


What's Next

Provider-specific error metadata: Anthropic rate limit errors include a retry-after header. OpenAI rate limit errors include a Retry-After header. Some errors include quota information. Surfacing these in LLMError.metadata as a dict would make the full error context accessible without digging into original_exception.

Streaming error normalization: streaming responses can fail mid-stream with errors that appear differently than synchronous call errors. Adding stream error parsing would cover the streaming use case.

Error frequency tracking: normalize_error() could optionally emit to a counter (by provider and kind) for observability. The counters would be per-process but useful for detecting error rate spikes.


Built as part of the agent-stack family: composable Python primitives for production LLM agents.