Skip to content

fix(anthropic): avoid quadratic stream folding allocation - #2678

Open
0YHR0 wants to merge 1 commit into
theagentrouter:mainfrom
0YHR0:perf/anthropic-stream-fold-builders
Open

0YHR0 wants to merge 1 commit into
theagentrouter:mainfrom
0YHR0:perf/anthropic-stream-fold-builders

Conversation

@0YHR0

@0YHR0 0YHR0 commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Description

Anthropic stream folding repeatedly concatenates growing text, thinking, and tool-argument strings. Each delta copies the entire accumulated prefix, causing quadratic allocation for long responses, including GenAI traces with message-content capture disabled.

Accumulate fragments in per-block strings.Builder buffers while preserving initial content, interleaved indices, block replacement, and tool argument parsing at block stop. Add regression tests and a fold-only benchmark. This removes repeated prefix copies but does not implement a bounded-memory recorder or stop retaining source chunks.

Related Issues/PRs (if applicable)

Concrete performance improvement supporting #2668. Independent of the proposed configuration and recorder-interface changes, and of the usage-alias fix in #2635.

Special notes for reviewers (if applicable)

Using the same sampled-span benchmark from #2668 (1 MiB text, 8,192 deltas, content capture off), cumulative allocation fell from 4,330,508,344 B/op to 8,048,784 B/op, approximately a 99.8% reduction. These are second runs with -benchtime=1x -count=2, not peak heap or statistically rigorous latency claims.

Local validation uses Go 1.26.6 / Windows amd64 in the isolated 1899d8d4 checkout used for the proposal baseline, with this implementation and tests applied. go test ./internal/apischema/anthropic ./internal/tracing -count=1 and the full-span benchmark pass. The PR is based on current main; its Go 1.27.1 validation remains for CI.

Developed with assistance from OpenAI Codex.

Signed-off-by: 0YHR0 <97868579@qq.com>
@0YHR0
0YHR0 requested a review from a team as a code owner September 12, 2026 02:31
@netlify

netlify Bot commented Sep 12, 2026

Copy link
Copy Markdown

Deploy Preview for theagentrouter canceled.

Name Link
🔨 Latest commit c63f7bc
🔍 Latest deploy log https://app.netlify.com/projects/theagentrouter/deploys/6aa4b97d7345c30008a9897b

@0YHR0 0YHR0 changed the title perf(anthropic): avoid quadratic stream folding allocation fix(anthropic): avoid quadratic stream folding allocation Sep 12, 2026
@codecov

codecov Bot commented Sep 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@missBerg missBerg added bug Something isn't working area/observability Metrics, tracing, logging, GenAI observability labels Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/observability Metrics, tracing, logging, GenAI observability bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants