feat(gateway): stream the Anthropic /v1/messages endpoint - #126
Merged
Conversation
Wire native SSE streaming for the Anthropic-compatible endpoint, completing the streaming surface that /v1/chat/completions already had. The provider's events are forwarded to the client verbatim (authentic Anthropic SSE, no translation) while the run is metered from the stream's own accounting: input_tokens from message_start, the final cumulative output_tokens from message_delta. Reuses the generic streamCall path, so the same guarantees apply: the budget is enforced before the stream opens, dollar/token usage is recorded after, and the run's context (time budget, kill switch, client disconnect) cuts a live stream. A provider whose backend doesn't implement streaming still returns a clear 501. Tests: provider-level SSE parse (verbatim forward + usage/model extraction, API-error and missing-key paths) and a gateway-level /v1/messages streaming test asserting verbatim forwarding and per-run metering.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Streaming (
stream:true) already worked on the OpenAI-compatible/v1/chat/completionspath; this wires it for the Anthropic-compatible/v1/messagespath so both surfaces stream, completing #22.The Anthropic provider now implements
provider.Streamer. Its events areforwarded to the client verbatim — authentic Anthropic SSE (
event:+data:lines), no translation — while the run is metered from the stream's ownaccounting:
input_tokensarrives onmessage_start(along with the model),the final cumulative
output_tokensonmessage_delta.The gateway path is unchanged:
/v1/messageswithstream:truenow routesthrough the same generic
streamCallthe OpenAI path uses, so every guaranteecarries over — the budget is enforced before the stream opens, dollar/token
usage is recorded after the stream closes (so the next call is refused if it
went over), and the run's context (time budget / kill switch / client
disconnect) cuts a live stream. A provider whose backend doesn't implement
streaming still returns a clear 501.
Also factored the system-prompt lifting and max-tokens defaulting shared by
ChatandChatStreaminto small helpers (splitSystem,anthropicMaxTokens).Tests
TestAnthropicChatStream— authentic Anthropic SSE in, verbatimbytes out, usage assembled (input from
message_start, final output frommessage_deltaoverriding the initial value), model extracted; plus theAPI-error and missing-key paths.
TestMessagesStreamingProxy_ForwardsAndMeters—/v1/messageswith
stream:trueforwards the Anthropic SSE verbatim and meters the callagainst the run.
go test -race ./...green;go vet ./...clean;gofmtclean.