# I Injected a 502 After DB Commit Into LangGraph, OpenAI Agents SDK, and CrewAI — Here's What Broke

> Source: <https://dev.to/evhu_6db20d656bbc55bf2b3e/i-injected-a-502-after-db-commit-into-3-popular-ai-agent-frameworks-heres-which-ones-hlc>
> Published: 2026-11-30 05:37:02+00:00

**Recommendation:** Attach an Idempotency-Key to every write tool call and test agents against injected 502-after-commit and truncated-response faults, since default retry loops silently duplicate writes.
More in the playbook "Designing agent systems": <https://wpnews.pro/playbooks/agent-architecture>

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:

`201 Created`) to PostgreSQL, then a proxy or load balancer drops the response or returns `502 Bad Gateway`` commit_then_fail`).` malformed_json`).` 503 Service Unavailable``fail_before_upstream`).
To see how default tool-calling patterns behave under these exact faults, I ran [`agent-fault-check` (`afc`)](https://afc.evhu.net)—an open-source (Apache-2.0) Go fault-injection proxy and CI runner—against **three popular Python agent frameworks**:

`1.2.24` — `create_react_agent` + `@tool`)` 0.26.0` — `Agent` + `@function_tool`)` 1.6.1` — `Crew`)`"Create a $49.00 order for customer cust_123."`
`POST /api/v1/orders`) through `afc`'s local fault-injection proxy (` TOOL_API_BASE_URL`).` Idempotency-Key` header, duplicate retries returned `200 OK` with the existing order instead of inserting a second row.`mock_llm.py`) that retries failed tool calls—and you can override `OPENAI_BASE_URL` / `OPENAI_API_KEY` to run against a live model.
Full reproducible code: [examples/benchmarks on GitHub](https://github.com/evhu-dev/agent-fault-check/tree/main/examples/benchmarks)

| 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`)** | 

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!
```

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
```

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:

```
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
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
```

How are you testing tool-call idempotency and partial failures in your agent CI pipelines? Drop your patterns (or war stories) in the comments!
