← Blog
November 9, 2026 · 11 min

When the handler declines a paid x402 request: an empty 200 from Gin and a header that leaks

A payment verifies. The handler runs. Then the handler says no. What does the buyer receive? In the Go Gin adapter for x402, on some of the ways a handler can say no, the answer is 200 OK with an empty body, no receipt and no charge.

That is the quietest failure an agent can meet. The status code says success. The body says nothing. Nothing was settled, so there is nothing to dispute. An agent loop that checks the status and parses the body fails one step later, far from the cause.

We probed the Go middleware on x402-foundation/x402 main (88b66dd, 8 November 2026) with a stub facilitator, then re-ran the same probe with the two open fixes applied. The probe also found a second, smaller problem on the same path: an internal header that reaches the client.

The decline path

The seller middleware has three moments that matter. It verifies the payment before the handler. It runs the handler with the response captured in a buffer. It settles after the handler, only if the response is a success.

A handler can decline in three ways. It can panic. It can return a status of 400 or higher. Or, in Gin, it can call c.Abort(). All three must end the same way: no settlement, and the handler's response goes to the buyer. We probed the second and third. The status-code path did. The abort path did not.

The adapter checks c.IsAborted() after the handler returns. In that branch it cancels the payment and builds the failure headers. Then it returns. It never restores c.Writer to the real writer and never writes the captured status or body. The bytes the handler wrote stay in the buffer and are dropped.

The status-code branch right below it does the restore and writes the capture. That asymmetry is the whole bug. PR #4188 adds the three missing lines. It was opened on 7 November 2026 and is open, review required, as we write.

The code is not new. The tag go/v2.30.0 (4 November 2026) is an ancestor of main and contains the same IsAborted() branch with the same early return. This shipped.

The probe

We used the package's own test doubles: a mock scheme server, a mock facilitator whose verify returns valid and whose settle counts calls, and a payment header. One route, GET /api, priced at $1.00. Seven handlers, each run once.

// one handler per row; settled = times facilitator.settle was called
r.GET("/api", func(c *gin.Context) {
    c.AbortWithStatusJSON(401, gin.H{"error": "nope"})
})

Results on main, 88b66dd:

main 88b66dd
  return c.JSON(200)  (control)       status=200 body=11 B  settled=1  Payment-Response: present
  c.JSON(500)                         status=500 body=16 B  settled=0
  AbortWithStatusJSON(401)            status=200 body=0 B   settled=0
  c.JSON(401); c.Abort()              status=200 body=0 B   settled=0
  c.JSON(200); c.Abort()              status=200 body=0 B   settled=0
  overrides + c.JSON(500)             status=500 body=16 B  settled=0  Settlement-Overrides: {}
  overrides + AbortWithStatusJSON(401) status=200 body=0 B  settled=0  Settlement-Overrides: {}

The control row is the contract. A success settles once and carries a Payment-Response header. A returned 500 reaches the buyer as a 500 and does not settle. The three abort rows are the break. The handler chose 401, or 401 again, or 200 with data, and the buyer saw 200 with zero bytes.

Nobody was charged on any of these rows. That is the good half. The buyer also got neither the resource nor an error, and a client that trusts the status code believes it was served.

What an agent sees

An agent that pays through x402 sees a 402, signs, retries, and gets a response. If the response is a 4xx or 5xx, it knows the call failed and, with a sound client, knows it was not charged. If it is a 200, the client hands the body to the model or the tool.

An empty 200 breaks that. A JSON parser raises on an empty body. A tool wrapper may return an empty string to the model as a valid result. Neither knows the seller declined. The agent may retry, sign again and be rejected, or move on with nothing. Our look at the double-sign problem in the fetch client shows how a client treats an uncertain outcome; here the server hides a certain one.

The decline status matters to the buyer in a second way. A 401 from the handler means "your credentials, not your payment". A 403 means "not allowed". A 429 means "slow down". All of that information was in the handler's response and all of it was dropped.

Running the fixes

Two companion PRs from the same author touch this path. We merged both onto main in a scratch clone (#4188 at 0186a95, #4189 at 0def917; the merge was clean) and re-ran the identical probe.

main + #4188 + #4189
  return c.JSON(200)  (control)       status=200 body=11 B  settled=1  Payment-Response: present
  c.JSON(500)                         status=500 body=16 B  settled=0
  AbortWithStatusJSON(401)            status=401 body=16 B  settled=0
  c.JSON(401); c.Abort()              status=401 body=16 B  settled=0
  c.JSON(200); c.Abort()              status=200 body=11 B  settled=0  no Payment-Response
  overrides + c.JSON(500)             status=500 body=16 B  settled=0  no override header
  overrides + AbortWithStatusJSON(401) status=401 body=16 B  settled=0  no override header

The 401 cases are fixed. The buyer sees the handler's status and body. The override header is gone. Good.

Look at the fifth row. c.JSON(200); c.Abort() used to produce an empty 200. Now it produces the full response, status 200, with no settlement and no Payment-Response. The content is delivered and never charged.

This is a design question, not a defect in the PR. The PR text says settlement "does not run on this path", and that is consistent. A handler that aborts is treated as declining, so nothing is settled. Before the fix, the empty body hid that a 200 abort delivers for free. After it, the body is visible. Any handler or downstream middleware that writes a success and then aborts, for example a response-cache middleware that serves a hit and stops the chain, now gives the resource away on a paid route.

Is that realistic? The PR cites two idioms from the repository as the reason this matters: the payment-identifier example at examples/go/servers/payment-identifier/main.go and the e2e Gin server. We read both. Each aborts in a middleware registered before the payment middleware, so neither goes through the broken branch. The example's comment says so: the payment middleware "only runs for requests that pass the idempotency check". The idiom is common. These two examples are not evidence of the bug.

The 200-then-abort case is still worth a decision. TypeScript adapters have no abort concept; a handler that responds 200 settles. In Gin the same response shape, plus an abort, now skips settlement. A seller can reasonably want either. The adapter should pick one and document it.

The header that leaks

The second finding is not about Gin alone. Settlement-Overrides is an internal header. A handler sets it with SetSettlementOverrides to change how much is settled, which is how the upto scheme reports metered usage. Its value is the JSON of a SettlementOverrides struct. Its one field is amount, which can be a raw amount, a percentage of the authorized maximum, or a dollar price.

The code that removes it before the response leaves the server lives in ProcessSettlement. The failure paths never call it. So when a handler sets the header and then fails, the header goes to the buyer. We saw Settlement-Overrides: {} on a 500 in our probe because we set empty overrides. A real handler would send the amount it had decided to charge.

The net/http adapter has a test in #4189 that checks this. Run against main, it fails:

TestPaymentMiddleware_StripsSettlementOverridesOnHandlerFailure
  middleware_test.go:1846: Expected Settlement-Overrides to be stripped, got "{}"
--- FAIL

The TypeScript adapters already strip it on the failure path. We found the removal at express/src/index.ts:363, fastify/src/index.ts:475, hono/src/index.ts:298 and next/src/utils.ts:226. Go was the outlier. The PR says both Python middlewares strip it too; we did not check those.

The leak is small. A failed call exposing a metering figure is not a breach. It is still a rule the specification already follows elsewhere, and it shows what happens when three copies of the same eighteen lines are edited by hand: they drift. #4189 moves the decision into one helper, ResolveCancelledSettlement, and leaves the framework-specific parts in each adapter.

Why three copies drift

#4189 counts eight sites across Gin, Echo and net/http. Each resolves the same decision: cancel the payment, build the failure receipt, set Cache-Control. The PR reports that 15 of the last 60 commits touching go/http changed all three middleware.go files together.

The two bugs here are the visible cost. One adapter forgot a write. All three forgot a delete. Neither is hard. Both are what happens when a rule lives in the same place three times.

Where it stands

What it means for LLM4Agents

We are a seller. Our gateway takes a paid request, verifies it, forwards it to a model provider, and settles on the result. The decline path is real for us: the provider can time out, return an error, or refuse after the payment has verified. In each case the buyer must see the provider-side failure, and no settlement should run.

The Gin finding is about one SDK, not our code, and we do not claim our gateway has this bug. What it shows is the shape of the contract we owe: on every decline, a status that matches the cause, the original error body, no settlement, and no internal headers. Our overview of the seller middleware stack shows where each SDK implements the request flow; the decline path is the part worth testing on its own.

It also bears on upto and metered billing. Buyer-side spend controls depend on what a buyer is told about a call. A leaked override on a failed call tells the buyer what the seller intended to charge, on a call it did not charge. For a metered LLM route that figure is a usage estimate. That is information we would rather not hand out by accident.

On the buyer side, the lesson is blunter. A paid route that returns 200 without a Payment-Response header is an anomaly. In our control row, the settled success carried one. An agent client that sees 200, no receipt and no body should not treat the call as served.

Staying on the frontier

  1. Add a decline-path matrix to our seller tests. Seven handlers like the table above: return an error, abort with a status, abort with a 200, panic, and each of those after setting overrides. Assert status, body, settle count and headers.
  2. Strip internal headers on every exit. Do it once, in the shared exit path, not at each return. Add a test that fails if Settlement-Overrides is present on any non-settled response.
  3. Decide the 200-then-abort rule. If a handler responds 200 and stops the chain, do we settle or not? Write the answer into the seller docs and make the middleware enforce it.
  4. Make our buyer client check for the receipt. A 200 on a paid route with no Payment-Response becomes an error the agent loop can act on, not a result.
  5. Pin Go dependencies to a commit, not a tag, until #4188 lands. Anyone embedding the Go middleware around a Gin route that aborts should add the three lines locally or avoid Abort after payment.
  6. Watch #4188, #4189 and the next Go release. When they merge, re-run the probe on the tagged version. This is cheap: one test file and a stub facilitator.

Build agents that know when a call was not served

Register an agent on LLM4Agents and pay per request in stablecoins over an OpenAI-compatible gateway.

Register agent