Skip to content

feat(agentic-server): relay streaming chat completions and meter their usage - #1768

Merged
pyramation merged 1 commit into
mainfrom
feat/gateway-streaming
Aug 19, 2026
Merged

feat(agentic-server): relay streaming chat completions and meter their usage#1768
pyramation merged 1 commit into
mainfrom
feat/gateway-streaming

Conversation

@pyramation

Copy link
Copy Markdown
Contributor

Summary

The gateway assumed every successful chat response was JSON (await upstream.json()), so any client asking for stream: true got SyntaxError: Unexpected token 'd', "data: {"id".... That is what a real harness does by default — found while pointing DeepSeek Harness at agentic-server as its OpenAI-compatible provider, where every turn failed.

/v1/chat/completions now branches on req.body.stream === true:

const body = streaming ? withUsageStreamOptions(transformed) : transformed;   // stream_options.include_usage
...
if (streaming) {
  const usage = await relayEventStream(upstream, res);   // frames forwarded verbatim
  sink?.logInference({ ...identity, inputTokens: usage?.prompt_tokens ?? 0, ... });
  return;
}

Three things worth knowing:

  • Usage is scanned out of the relayed bytes, not parsed from a body. UsageScanner buffers across chunk boundaries, splits on \n, skips [DONE]/keep-alives/unparseable frames, and keeps the last usage object — so metering still fires on a streamed turn. stream_options: { include_usage: true } is added only when the caller expressed no preference.
  • Non-OpenAI streaming is rejected 501, not silently downgraded. resolveUpstreamUrl rewrites ollama/anthropic to their native endpoints, whose stream frames are not OpenAI chunks; answering a whole JSON body to a client that asked for a stream would be worse than failing loud.
  • Disconnects abort upstream via res.on('close'), not req. The request stream's close fires as soon as the body is consumed, so aborting on it kills every stream mid-flight (AbortError: This operation was aborted — observed before the fix). The error path also end()s instead of writing JSON once res.headersSent.

Verified end to end beyond the unit tests: DeepSeek Harness, running Constructive's 18 neutral @agentic-kit/db-tools through a local adapter, completed a tool-calling turn routed through this gateway, with the injected sink recording promptTokens/completionTokens for the streamed turn.

Link to Devin session: https://app.devin.ai/sessions/2d292e43fd2e4f34bb84605b5aa2a250
Requested by: @pyramation

@pyramation pyramation self-assigned this Aug 19, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@pyramation
pyramation merged commit 4abad90 into main Aug 19, 2026
20 checks passed
@pyramation
pyramation deleted the feat/gateway-streaming branch August 19, 2026 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant