From 9d4a4bfd3fc6bf0920bed447b4f04577a80f5cf8 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 31 Oct 2026 04:35:16 +0800 Subject: [PATCH] gateway: a late Claude Code call tells the client after 500ms, not 1s In Claude Code's interactive mode a call of a client's tool waits callFirst for its message in the transcript before the call tells the client itself. Measured on a real account (a probe build logging the time from the MCP call to the transcript's tool_use), a message written before its call was read 118 to 162ms after the call came in, 8 calls of 8. 500ms leaves it room and halves what a late message costs. Run: go test -race -count=3 -run 'TestClaudeTUI|TestSubscription' ./internal/gateway on mote, ok. Real account (claude-haiku-5-5, sandbox HOME, interactive TUI): 9 runs of the one/two/text scenarios, all ok, every call told once with its own id; 1 took the late path, none fell back to -p. Not tried: sonnet/opus, Linux. --- docs/subsystems/claude-subscription-bridge.md | 3 ++- internal/gateway/claude_subscription.go | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 0bba11287..29d118c39 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -121,7 +121,8 @@ answered: on a real account, 4 runs of 8 wrote it after its MCP call returned. The call can only be answered by the client, which hasn't been told of it. So in an interactive run, `mcpCall` hands each call to the reader (`run.onCall`). Not shown from the transcript within `callFirst` -(1s), the call is a reply of its own: a message with that one `tool_use` +(500ms; a message written before its call is read 118 to 162ms after +it), the call is a reply of its own: a message with that one `tool_use` (Claude Code makes its MCP calls one at a time, a message's parallel calls too). The first message written after such a call is the one that made it. Its rows are left out (`lateID`), and its usage is told with the diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index 0a1370f65..c7041a63e 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -2847,8 +2847,11 @@ var unstreamedQuiet = 300 * time.Millisecond // callFirst is how long a call of a client's tool in Claude Code's // interactive mode waits for its message in the transcript before the call -// tells the client itself (readOutput). -var callFirst = time.Second +// tells the client itself (readOutput). A message written before its call +// was read 118 to 162ms after the call came in (8 calls on a real account), +// so half a second leaves it room and makes a late message cost half as +// long as it did at 1s. +var callFirst = 500 * time.Millisecond type cliUsage struct { Input int `json:"input_tokens"`