Skip to content

Build and run agents you can see, understand and trust.

fix(middleware): retire the budget hint at a new reply boundary (#3599)

Felix Shi9aeab115940b
diff --git a/src/agentscope/middleware/_budget.py b/src/agentscope/middleware/_budget.py
index a34ae5049..6115f7cbf 100644
--- a/src/agentscope/middleware/_budget.py
+++ b/src/agentscope/middleware/_budget.py
@@ -25,6 +25,43 @@ _STRUCTURED_OUTPUT_HINT_MESSAGE = (
"tools.</system-reminder>"
)
+# Marks the wrap-up hints this middleware injects. They live in the persisted
+# context, so a new reply -- which starts with a fresh budget and no forced
+# tool_choice -- has to be able to find and retire the previous reply's hints.
+_BUDGET_HINT_SOURCE = (
+ '{"label": "System", "sublabel": "Reply Budget Exhausted"}'
+)
+
+
+def _retire_budget_hints(agent: "Agent") -> None:
+ """Remove wrap-up hints this middleware injected for an earlier reply.
+
+ Only hints carrying :data:`_BUDGET_HINT_SOURCE` are removed, so hints from
+ other middleware, and every message that still has real content, stay put.
+ A message left with no content at all was created solely to carry the hint
+ and is dropped with it.
+ """
+ context = agent.state.context
+ kept = []
+ for message in context:
+ content = getattr(message, "content", None)
+ if isinstance(content, list):
+ remaining = [
+ block
+ for block in content
+ if not (
+ isinstance(block, HintBlock)
+ and block.source == _BUDGET_HINT_SOURCE
+ )
+ ]
+ if len(remaining) != len(content):
+ if not remaining:
+ continue
+ message.content = remaining
+ kept.append(message)
+ if len(kept) != len(context):
+ context[:] = kept
+
class ReplyBudgetControlMiddleware(MiddlewareBase):
"""Middleware that enforces a weighted token budget per reply.
@@ -132,6 +169,9 @@ class ReplyBudgetControlMiddleware(MiddlewareBase):
async for event in next_handler(**input_kwargs):
if isinstance(event, ReplyStartEvent):
+ # A new reply starts with a fresh budget, so the previous
+ # reply's wrap-up instruction must not leak into it.
+ _retire_budget_hints(agent)
# Start counting for this reply, dropping the previous one.
# Not cleaned on ReplyEndEvent: an outer middleware may
# swallow it and the same reply then keeps spending tokens
@@ -201,6 +241,7 @@ class ReplyBudgetControlMiddleware(MiddlewareBase):
if structured_output_pending
else self.hint_message
),
+ source=_BUDGET_HINT_SOURCE,
)
if (
len(agent.state.context) > 0
diff --git a/tests/middleware_budget_test.py b/tests/middleware_budget_test.py
index 5f1d470f6..6f0f9d8a8 100644
--- a/tests/middleware_budget_test.py
+++ b/tests/middleware_budget_test.py
@@ -408,6 +408,63 @@ class TestBudgetControlMiddleware(IsolatedAsyncioTestCase):
{agent.state.reply_id: 28},
)
+ async def test_new_reply_does_not_inherit_the_previous_budget_hint(
+ self,
+ ) -> None:
+ """A wrap-up hint belongs to one reply; the next reply starts clean.
+
+ Reply 1 spends its whole 300-token budget on the first step, so the
+ second step runs under the hint. Reply 2 starts with a fresh counter
+ and must not see that wrap-up instruction again.
+ """
+ toolkit = Toolkit(tools=[DummyTool()])
+
+ model = MockModel()
+ model.set_responses(
+ [
+ ChatResponse(
+ content=[
+ ToolCallBlock(id="tc_1", name="dummy", input="{}"),
+ ],
+ is_last=True,
+ usage=ChatUsage(
+ input_tokens=200,
+ output_tokens=100,
+ time=0.0,
+ ),
+ ),
+ _response("wrapped up", input_tokens=5, output_tokens=5),
+ _response("fresh reply", input_tokens=5, output_tokens=5),
+ ],
+ )
+
+ middleware = ReplyBudgetControlMiddleware(token_budget=300)
+ agent = Agent(
+ name="test_agent",
+ system_prompt="you are helpful",
+ model=model,
+ toolkit=toolkit,
+ middlewares=[middleware],
+ )
+
+ await agent.reply(UserMsg("user", "first"))
+ self.assertTrue(
+ any(
+ _has_hint_block(message, middleware.hint_message)
+ for message in agent.state.context
+ ),
+ "the exhausted reply should have injected the wrap-up hint",
+ )
+
+ await agent.reply(UserMsg("user", "second"))
+ self.assertFalse(
+ any(
+ _has_hint_block(message, middleware.hint_message)
+ for message in agent.state.context
+ ),
+ "a fresh reply must not carry the previous reply's budget hint",
+ )
+
async def test_swallowed_reply_end_keeps_counting(self) -> None:
"""A redo round forced by swallowing the ReplyEndEvent spends the
budget of the same reply."""