Apply the context trim to streamed runs - #667
Open
elias-ba wants to merge 1 commit into
Open
Conversation
elias-ba
force-pushed
the
fix/context-management-on-streaming
branch
from
September 4, 2026 23:22
e72f9ba to
2b2aa74
Compare
context_management was passed only to the non-streaming call. Lightning sends options.stream true to /services/global_chat/stream, global_chat reads that flag and the router hands it to the planner, so every real global chat request takes the streaming branch and no trim was ever applied to it. Raising the tool budget to 20 doubled how far that conversation can grow with nothing bounding it. The streaming call moves to the beta client, which accepts betas and context_management, and both branches now share one config so they cannot drift apart again.
elias-ba
force-pushed
the
fix/context-management-on-streaming
branch
from
September 4, 2026 23:26
2b2aa74 to
2f4248b
Compare
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.
Short Description
context_managementwas only passed on the planner's non-streaming call. Global chat streams, so the config only ever reached a path production does not take. Both calls now share one definition.Implementation Details
Lightning sends
"options" => {"stream" => true}to/services/global_chat/stream(apollo_client.ex:280),global_chatreads that flag and the router hands it to the planner, so_call_apialways takes the streaming branch in production. That branch calledself.client.messages.streamon the non-beta client with nobetasand nocontext_management, while the non-streaming branch below it had both. The streaming call moves toself.client.beta.messages.stream, which accepts both, and the config becomes one shared constant so the two cannot drift apart again.Worth being straight about what this does not do. With
max_tool_callsat 20 and the trigger at 40 tool uses, the trim cannot fire on either path: a run tops out around 20 plus a small parallel overshoot, andreturn_historycarries no tool_use blocks so nothing accumulates across turns. This is a backstop if the budget rises, not something that bounds a run today. Actually bounding it means putting the trigger below the budget and protecting the wrap-up round some other way, or measuring the trigger ininput_tokensrather than tool uses. That is a separate change and I have not made it here.The unit tests patch
_call_api, so nothing exercises the new request shape and this is now the only path that runs. I sent a real streaming request againstclaude-opus-4-8with the beta header,context_management, tools, thinking andoutput_config: it returnedend_turnwithBetaContextManagementResponse(applied_edits=[]), so the shape is accepted and, as expected, nothing was cleared.AI Usage
Please disclose whether you've used AI in this work (it's cool, we just want to know!):
You can read more details in our Responsible AI Policy