kapil971390 d050fba07f
fix(token-budget): acquire _lock in _apply, before_agent, and _drain_pending_warnings (#3714)
_lock was defined in __init__ and used only in reset(), leaving _apply(),
before_agent(), _clear_run_state(), and _drain_pending_warnings() unprotected.
Concurrent after_model callbacks for the same run_id could race on
usage_accum.input += diff_input, causing lost-update on token counters.

Also fix stray bare 'a' character in frontend/src/content/zh/introduction/index.mdx
that rendered as visible text on the documentation page.

Co-authored-by: kapilvus <kapilvus@gmail.com>
2026-06-22 20:44:07 +08:00

291 lines
12 KiB
Python

"""Middleware to enforce per-run token budget limits.
Tracks cumulative token usage (input, output, total) across model calls within
a single agent run and enforces configurable soft-warning and hard-stop
thresholds.
Detection strategy:
1. After each model response, sum the `usage_metadata` of all `AIMessage`s
in the current thread history. This automatically captures tokens from
subagents because `TokenUsageMiddleware` retroactively adds them to the
history.
2. If the highest fraction (input, output, or total) >= warn_threshold,
queue a warning.
3. If the highest fraction >= hard_stop_threshold, strip tool_calls.
Warning injection uses the deferred pattern:
- after_model queues the warning (does NOT mutate state).
- wrap_model_call injects it as a HumanMessage at the next model call.
This preserves AIMessage(tool_calls) → ToolMessage pairing.
"""
from __future__ import annotations
import logging
import threading
from collections import OrderedDict
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from typing import Any, override
from langchain.agents import AgentState
from langchain.agents.middleware import AgentMiddleware
from langchain.agents.middleware.types import ModelCallResult, ModelRequest, ModelResponse
from langchain_core.messages import AIMessage, HumanMessage
from langgraph.runtime import Runtime
from deerflow.config.token_budget_config import TokenBudgetConfig
logger = logging.getLogger(__name__)
_BUDGET_WARNING_MSG = (
"[TOKEN BUDGET WARNING] You have used {used:,} of your {budget:,} {reason} token budget ({percent:.0f}%). Wrap up your current work and produce a final answer. Avoid starting new tool calls unless absolutely necessary."
)
_BUDGET_EXCEEDED_MSG = "[TOKEN BUDGET EXCEEDED] The {reason} token usage ({used:,}) has exceeded the safety limit ({budget:,}). Producing final answer with results collected so far."
@dataclass
class TokenUsage:
input: int = 0
output: int = 0
total: int = 0
class BoundedDict(OrderedDict):
"""A bounded dictionary to prevent unbounded state growth on abandoned runs."""
def __init__(self, maxsize=1000, *args, **kwds):
self.maxsize = maxsize
super().__init__(*args, **kwds)
def __setitem__(self, key, value):
if key not in self:
if len(self) >= self.maxsize:
self.popitem(last=False)
super().__setitem__(key, value)
class TokenBudgetMiddleware(AgentMiddleware[AgentState]):
"""Enforce per-run token budget limits."""
def __init__(self, config: TokenBudgetConfig) -> None:
super().__init__()
self._config = config
self._lock = threading.Lock()
# Keyed strictly by run_id (clobber-safe) and bounded (leak-safe)
self._warned: BoundedDict[str, bool] = BoundedDict(1000)
self._pending_warnings: BoundedDict[str, list[str]] = BoundedDict(1000)
self._seen_messages: BoundedDict[str, dict[str, tuple[int, int]]] = BoundedDict(1000)
self._cumulative_usage: BoundedDict[str, TokenUsage] = BoundedDict(1000)
@classmethod
def from_config(cls, config: TokenBudgetConfig) -> TokenBudgetMiddleware:
return cls(config=config)
def reset(self) -> None:
with self._lock:
self._warned.clear()
self._pending_warnings.clear()
self._seen_messages.clear()
self._cumulative_usage.clear()
@staticmethod
def _get_run_id(runtime: Runtime) -> str:
ctx = getattr(runtime, "context", None)
if isinstance(ctx, dict) and "run_id" in ctx:
return ctx["run_id"]
# Fallback to runtime object ID to prevent collisions across embedded client runs
return str(id(runtime))
def _clear_run_state(self, run_id: str) -> None:
with self._lock:
self._warned.pop(run_id, None)
self._pending_warnings.pop(run_id, None)
self._seen_messages.pop(run_id, None)
self._cumulative_usage.pop(run_id, None)
@override
def before_agent(self, state: AgentState, runtime: Runtime) -> None:
if not self._config.enabled:
return
# Mark all old messages from previous runs as 'seen' so they don't count toward THIS run's budget
messages = state.get("messages", [])
if not messages:
return
run_id = self._get_run_id(runtime)
with self._lock:
seen = self._seen_messages.setdefault(run_id, {})
self._cumulative_usage.setdefault(run_id, TokenUsage())
for msg in messages:
if isinstance(msg, AIMessage) and msg.id and hasattr(msg, "usage_metadata"):
usage = msg.usage_metadata or {}
input_tokens = usage.get("input_tokens", 0)
output_tokens = usage.get("output_tokens", 0)
seen[msg.id] = (input_tokens, output_tokens)
@override
async def abefore_agent(self, state: AgentState, runtime: Runtime) -> None:
self.before_agent(state, runtime)
@override
def after_agent(self, state: AgentState, runtime: Runtime) -> None:
if not self._config.enabled:
return
self._clear_run_state(self._get_run_id(runtime))
@override
async def aafter_agent(self, state: AgentState, runtime: Runtime) -> None:
self.after_agent(state, runtime)
@staticmethod
def _append_text(content: str | list[dict | None] | None, stop_msg: str) -> str | list[dict | str]:
"""Append a stop message to an AIMessage.content field."""
if content is None:
return stop_msg
if isinstance(content, str):
if content:
return f"{content}\n\n{stop_msg}"
return f"\n\n{stop_msg}"
if isinstance(content, list):
new_content = list(content)
new_content.append({"type": "text", "text": f"\n\n{stop_msg}"})
return new_content
return f"{content}\n\n{stop_msg}"
def _build_hard_stop_update(self, msg: AIMessage, stop_msg: str) -> dict[str, Any]:
"""Build the state update dictionary for a hard stop."""
updated_content = self._append_text(msg.content, stop_msg)
kwargs = dict(msg.additional_kwargs) if msg.additional_kwargs else {}
if "tool_calls" in kwargs:
del kwargs["tool_calls"]
if "function_call" in kwargs:
del kwargs["function_call"]
response_metadata = dict(getattr(msg, "response_metadata", {}) or {})
if response_metadata.get("finish_reason") == "tool_calls":
response_metadata["finish_reason"] = "stop"
stopped_msg = msg.model_copy(update={"content": updated_content, "tool_calls": [], "additional_kwargs": kwargs, "response_metadata": response_metadata})
return {"messages": [stopped_msg]}
def _apply(self, state: AgentState, runtime: Runtime) -> dict | None:
if not self._config.enabled:
return None
messages = state.get("messages", [])
if not messages:
return None
last_msg = messages[-1]
if not isinstance(last_msg, AIMessage):
return None
run_id = self._get_run_id(runtime)
with self._lock:
seen = self._seen_messages.setdefault(run_id, {})
usage_accum = self._cumulative_usage.setdefault(run_id, TokenUsage())
for msg in messages:
if isinstance(msg, AIMessage) and msg.id and hasattr(msg, "usage_metadata"):
usage = msg.usage_metadata or {}
input_tokens = usage.get("input_tokens", 0)
output_tokens = usage.get("output_tokens", 0)
# Check what previously recorded for this exact message
prev_input, prev_output = seen.get(msg.id, (0, 0))
# Calculate if any new tokens were added (handles retroactive subagent tokens)
diff_input = max(0, input_tokens - prev_input)
diff_output = max(0, output_tokens - prev_output)
if diff_input > 0 or diff_output > 0:
usage_accum.input += diff_input
usage_accum.output += diff_output
usage_accum.total += diff_input + diff_output
seen[msg.id] = (input_tokens, output_tokens)
if usage_accum.total <= 0:
return None
fractions = [("total", usage_accum.total, self._config.max_tokens)]
if self._config.max_input_tokens:
fractions.append(("input", usage_accum.input, self._config.max_input_tokens))
if self._config.max_output_tokens:
fractions.append(("output", usage_accum.output, self._config.max_output_tokens))
highest_fraction = 0.0
trigger_reason = ""
trigger_used = 0
trigger_budget = 0
for reason, used, limit in fractions:
frac = used / limit
if frac > highest_fraction:
highest_fraction = frac
trigger_reason = reason
trigger_used = used
trigger_budget = limit
if highest_fraction >= self._config.hard_stop_threshold:
logger.warning("Token budget hard stop triggered for run %s: %s limit exceeded", run_id, trigger_reason)
stop_text = _BUDGET_EXCEEDED_MSG.format(reason=trigger_reason, used=trigger_used, budget=trigger_budget)
return self._build_hard_stop_update(last_msg, stop_text)
if highest_fraction >= self._config.warn_threshold and not self._warned.get(run_id, False):
self._warned[run_id] = True
percent = highest_fraction * 100
warn_text = _BUDGET_WARNING_MSG.format(reason=trigger_reason, used=trigger_used, budget=trigger_budget, percent=percent)
logger.info("Token budget warning triggered for run %s: %s limit at %.1f%%", run_id, trigger_reason, percent)
# queue warning for wrap_model_call
warnings = self._pending_warnings.setdefault(run_id, [])
warnings.append(warn_text)
return None
return None
@override
def after_model(self, state: AgentState, runtime: Runtime) -> dict | None:
return self._apply(state, runtime)
@override
async def aafter_model(self, state: AgentState, runtime: Runtime) -> dict | None:
return self._apply(state, runtime)
def _drain_pending_warnings(self, runtime: Runtime) -> list[str]:
if not self._config.enabled:
return []
run_id = self._get_run_id(runtime)
with self._lock:
warnings = self._pending_warnings.pop(run_id, None)
return warnings or []
def _inject_warnings(self, request: ModelRequest, warnings: list[str]) -> ModelRequest:
if not warnings:
return request
merged_text = "\n\n".join(warnings)
warning_msg = HumanMessage(content=merged_text, name="budget_warning")
messages = getattr(request, "messages", [])
new_messages = list(messages) + [warning_msg]
return request.override(messages=new_messages)
@override
def wrap_model_call(self, request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]) -> ModelCallResult:
warnings = self._drain_pending_warnings(request.runtime)
request = self._inject_warnings(request, warnings)
return handler(request)
@override
async def awrap_model_call(self, request: ModelRequest, handler: Callable[[ModelRequest], Awaitable[ModelResponse]]) -> ModelCallResult:
warnings = self._drain_pending_warnings(request.runtime)
request = self._inject_warnings(request, warnings)
return await handler(request)