sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[python] ContextManager: utilization is re-estimated without tool specs/system prompt after a strategy acts

Open tyx022898 opened this issue 3 weeks ago • 2 comments

Package: strands-agents (Python) 1.64.0, strands/_context_manager/context_manager.py

What happens

After a context strategy changes the history, ContextManager recomputes utilization with agent.model.count_tokens(messages) only, without the tool specs or the system prompt that the first estimate counted. Messages that carried provider usage metadata also keep it, so a later usage-derived estimate can still read the pre-strategy size. The two estimates are therefore on different bases: after a strategy acts, utilization reads lower than what the next request will actually send (by the size of the tool list and system prompt, which for an agent with large tool schemas is tens of thousands of characters). Explicit compaction, which applies a strategy outside the proactive/overflow pipeline, does not refresh the estimate at all.

Expected

One method applies a strategy and, when it acted, (1) drops the stale usage entries from the messages' metadata and (2) re-counts with the same inputs as the first estimate:

async def apply_strategy(self, strategy: ContextStrategy, context: ContextState) -> bool:
    acted = await strategy.apply(context)
    if acted:
        for message in context.messages:
            if metadata := message.get("metadata"):
                metadata = metadata.copy()
                metadata.pop("usage", None)
                message["metadata"] = metadata
        agent = context.agent
        tokens = await agent.model.count_tokens(
            context.messages,
            tool_specs=agent.tool_registry.get_all_tool_specs(),
            system_prompt=agent.system_prompt,
            system_prompt_content=agent._system_prompt_content,
        )
        context.utilization = agent.model.estimate_utilization(tokens)
    return acted

and the pipeline calls await self.apply_strategy(strategy, context) instead of await strategy.apply(context) followed by the partial recount. We run this as a local patch on 1.64.0; it is checked on main as of 2026-11-09 and the recount there is still count_tokens(messages) alone. Happy to open a PR if the shape is acceptable.

tyx022898 avatar Nov 10 '26 01:11 tyx022898

Happy to pick this up. As I read it, the post-strategy utilization estimate should be computed on the same basis as the initial one — the messages plus the tool specs and system prompt the next request will actually send — rather than messages alone. I'll reproduce the discrepancy first, then send a fix with tests.

karmendra8386 avatar Nov 17 '26 02:11 karmendra8386

One addition to my note above: I saw the issue includes a proposed patch shape and an offer to open the PR. If the author would prefer to send that PR themselves, I'm happy to step aside — just say so. Otherwise I'll follow that shape, since it matches how the first estimate is computed.

karmendra8386 avatar Nov 17 '26 02:11 karmendra8386