I Injected a 502 After DB Commit Into LangGraph, OpenAI Agents SDK, and CrewAI - Here's What Broke
Most AI agent benchmarks test prompt accuracy or mock happy-path tool responses. In production, the bugs that wake engineers up at 2 AM aren't prompt typos - they're distributed-systems failures between your agent and your HTTP tool APIs:
- An API commits a write (
201 Created) to PostgreSQL, then a proxy or load balancer drops the response or returns502 Bad Gateway(commit_then_fail). - An API commits a write, then returns truncated/malformed JSON (
malformed_json). - An API returns a transient
503 Service Unavailablebefore committing (fail_before_upstream).
To see how default tool-calling patterns behave under these exact faults, I ran agent-fault-check (afc) - an open-source (Apache-2.0) Go fault-injection proxy and CI runner - against three popular Python agent frameworks:
- LangGraph (
1.2.24-create_react_agent+@tool) - OpenAI Agents SDK (
0.26.0-Agent+@function_tool) - CrewAI (
1.6.1-Agent+@tool+Crew)
Test Setup (Fair & 100% Reproducible)
- Every agent received the same prompt: "Create a $49.00 order for customer cust_123."
- Every agent called the same local Orders HTTP API (
POST /api/v1/orders) throughafc's local fault-injection proxy (TOOL_API_BASE_URL). - The Orders API supported idempotency: if the agent sent an
Idempotency-Keyheader, duplicate retries returned200 OKwith the existing order instead of inserting a second row. - To make the benchmark runnable in 60 seconds with $0 API cost, the repo ships with a deterministic OpenAI-compatible mock LLM (
mock_llm.py) that retries failed tool calls - and you can overrideOPENAI_BASE_URL/OPENAI_API_KEYto run against a live model.
Full reproducible code: examples/benchmarks on GitHub
The Results: Two Opposite Failure Modes
| Framework (Version) | Mode | commit_then_fail (502 after 201) | malformed_json (truncated 201) | fail_before_upstream (503) |
|---|---|---|---|---|
| LangGraph (1.2.24) | Default | 2 rows (DUPLICATE WRITE) | 2 rows (DUPLICATE WRITE) | 1 row (PASS) |
| LangGraph (1.2.24) | Fixed (Idempotency-Key) | 1 row (PASS, 200 dedup) | 1 row (PASS, 200 dedup) | 1 row (PASS) |
| OpenAI Agents SDK (0.26.0) | Default | 2 rows (DUPLICATE WRITE) | 2 rows (DUPLICATE WRITE) | 1 row (PASS) |
| OpenAI Agents SDK (0.26.0) | Fixed (Idempotency-Key) | 1 row (PASS, 200 dedup) | 1 row (PASS, 200 dedup) | 1 row (PASS) |
| CrewAI (1.6.1) | Default | 1 row, 0 retries (EXIT 1) | 1 row, 0 retries | 0 rows (DROPPED ORDER, EXIT 1) |
| CrewAI (1.6.1) | Fixed (In-tool retry + Idempotency-Key) | 1 row (PASS, 200 dedup) | 1 row (PASS, 200 dedup) | 1 row (PASS) |
Failure Mode 1: Silent Duplicate Writes (LangGraph & OpenAI Agents SDK)
Both LangGraph and OpenAI Agents SDK did what you'd expect when a tool returned HTTP error 502 or a JSONDecodeError: the LLM loop retried create_order(customer_id="cust_123", amount_cents=4900).
Because standard tutorial tool functions don't attach an Idempotency-Key, the second call silently inserted a second order row in the database - even on malformed_json:
[commit-then-fail-502] (LangGraph / OpenAI Agents SDK - default mode)
timeline:
#1 POST /api/v1/orders -> upstream 201 -> injected 502 [lose-first-commit]
#2 POST /api/v1/orders -> upstream 201 <-- SECOND ORDER WRITTEN!
Failure Mode 2: Silent Dropped Orders (CrewAI)
CrewAI surprised me. It did not double-charge on 502 - not because it sent an idempotency key, but because CrewAI's executor blocks consecutive identical tool calls as a loop guard (and caches tool outputs by default).
The catch? When the API returned a completely recoverable transient 503 Service Unavailable before writing anything (fail_before_upstream), CrewAI refused to retry and finished with 0 orders written:
[failed] fail-before-upstream-503 (CrewAI - default mode)
timeline:
#1 POST /api/v1/orders -> injected 503 [transient-503]
- FAIL (operation_attempts): expected >= 1 retry attempts with X-Operation-ID="op-crew-..." after "transient-503", got 0
- PASS (max_tool_calls): tool calls for POST /api/v1/orders within budget (1 <= 3)
hint: Verify the agent retries transient tool failures with the same correlation header instead of silently giving up.
Exit code: 1
The 10-Line Production Fix
Frameworks shouldn't be blamed for missing business-level idempotency - that's application logic. The fix that turned all 3 frameworks green (1 row written, EXIT 0 across every fault) has two parts:
- Derive a deterministic
Idempotency-Keyfrom the run/operation ID and semantic arguments (owned by your code, never minted by the LLM): - Keep bounded transient HTTP retries inside the tool wrapper (mandatory for CrewAI, and cheaper/faster than burning LLM tokens on retries in LangGraph and OpenAI Agents SDK):
headers["Idempotency-Key"] = f"order-{run_id}-{customer_id}-{amount_cents}"
for _ in range(3):
resp = httpx.post(f"{TOOL_BASE_URL}/api/v1/orders", json={"customer_id": customer_id, "amount_cents": amount_cents}, headers=headers, timeout=5.0,)
if resp.status_code < 400:
return json.dumps(resp.json())
With that fix, afc verifies deduplication (#2 -> upstream 200) and recovery across all 3 frameworks:
[passed] commit-then-fail-502 (754 ms)
timeline:
#1 POST /api/v1/orders -> upstream 201 -> injected 502 [lose-first-commit]
#2 POST /api/v1/orders -> upstream 200 <-- DEDUPLICATED!
- PASS (operation_attempts): observed 1 retry attempts after "lose-first-commit"
- PASS (max_tool_calls): tool calls for POST /api/v1/orders within budget (2 <= 3)
Exit code: 0
Reproduce It Yourself in 60 Seconds
git clone https://github.com/evhu-dev/agent-fault-check.git
cd agent-fault-check
go build -o /tmp/afc ./cmd/afc
uv venv --python 3.12 .bench-venv
uv pip install --python .bench-venv/bin/python langgraph langchain-openai openai-agents crewai httpx pyyaml
AFC_BIN=/tmp/afc BENCH_PYTHON=.bench-venv/bin/python .bench-venv/bin/python examples/benchmarks/run_benchmark.py
Links
- Live Benchmark Report: https://afc.evhu.net/benchmarks.html
- GitHub (Apache-2.0): https://github.com/evhu-dev/agent-fault-check
- Docs & GitHub Action Guide: https://afc.evhu.net/docs.html
How are you testing tool-call idempotency and partial failures in your agent CI pipelines? Drop your patterns (or war stories) in the comments!
Comments
No comments yet. Start the discussion.