Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(agents): count the full request in the context-window guard

cgycorey8df56e7dfe8b
diff --git a/deeptutor/agents/loop/agent_loop.py b/deeptutor/agents/loop/agent_loop.py
index 7f1a76821..14defd0a5 100644
--- a/deeptutor/agents/loop/agent_loop.py
+++ b/deeptutor/agents/loop/agent_loop.py
@@ -1047,7 +1047,9 @@ class AgentLoop:
tool_choice: str | None = None,
) -> LLMCallResult:
request_messages = with_transient_model_messages(messages, self._transient_model_messages)
- await self.pipeline._guard_context_window(request_messages, self.stream)
+ await self.pipeline._guard_context_window(
+ request_messages, self.stream, tool_schemas=tool_schemas
+ )
stage = self.stage
call_id = new_call_id(f"{self.source}-{stage}")
trace_meta = build_trace_metadata(
diff --git a/deeptutor/agents/loop/pipeline.py b/deeptutor/agents/loop/pipeline.py
index fe4fb6c62..089bcb481 100644
--- a/deeptutor/agents/loop/pipeline.py
+++ b/deeptutor/agents/loop/pipeline.py
@@ -1624,6 +1624,7 @@ class AgenticLoopPipeline:
self,
messages: list[dict[str, Any]],
stream: StreamBus,
+ tool_schemas: list[dict[str, Any]] | None = None,
) -> None:
try:
window = resolve_effective_context_window(
@@ -1636,19 +1637,31 @@ class AgenticLoopPipeline:
if not window or window <= 0:
return
budget = int(window * CONTEXT_WINDOW_GUARD_RATIO)
- if self._estimate_messages_tokens(messages) <= budget:
+ # Each message is counted once; the running total moves as tool results are snipped.
+ counts = [self._request_message_tokens(msg) for msg in messages]
+ total = sum(counts) + self._tool_schema_tokens(tool_schemas)
+ if total <= budget:
return
+ marker = self._tool_result_snip_marker()
snipped = False
- for msg in messages:
- if msg.get("role") != "tool":
- continue
- marker = self._tool_result_snip_marker()
- if msg.get("content") == marker:
+ for index, msg in enumerate(messages):
+ if msg.get("role") != "tool" or msg.get("content") == marker:
continue
msg["content"] = marker
snipped = True
- if self._estimate_messages_tokens(messages) <= budget:
+ after = self._request_message_tokens(msg)
+ total += after - counts[index]
+ counts[index] = after
+ if total <= budget:
break
+ if total > budget:
+ logger.warning(
+ "Context-window guard: request is about %d tokens against a %d-token budget "
+ "(window %d) and no tool results are left to trim; sending it as-is",
+ total,
+ budget,
+ window,
+ )
if snipped:
await stream.progress(
self._t("notices.context_window_guard"),
@@ -1687,22 +1700,27 @@ class AgenticLoopPipeline:
logger.debug("deferred-tool count probe failed", exc_info=True)
return 0
+ @staticmethod
+ def _request_message_tokens(message: dict[str, Any]) -> int:
+ """Tokens one message costs in the request, counted as history planning counts it."""
+ from deeptutor.services.session.context_builder import _count_model_context_tokens
+
+ # ``_call_llm`` strips this bookkeeping key before the provider sees the message.
+ sent = {key: value for key, value in message.items() if key != "_context_snapshot"}
+ return _count_model_context_tokens(sent)
+
+ @staticmethod
+ def _tool_schema_tokens(tool_schemas: list[dict[str, Any]] | None) -> int:
+ """Tokens the ``tools`` payload costs when the request carries it."""
+ if not tool_schemas:
+ return 0
+ from deeptutor.services.session.context_builder import _count_model_context_tokens
+
+ return _count_model_context_tokens(tool_schemas)
+
@staticmethod
def _estimate_messages_tokens(messages: list[dict[str, Any]]) -> int:
- from deeptutor.services.session.context_builder import count_tokens
-
- total = 0
- for msg in messages:
- content = msg.get("content")
- if isinstance(content, str):
- total += count_tokens(content)
- elif isinstance(content, list):
- for part in content:
- if isinstance(part, dict) and part.get("type") == "text":
- total += count_tokens(str(part.get("text") or ""))
- elif isinstance(part, dict) and part.get("type") == "image_url":
- total += IMAGE_TOKEN_GUARD_RESERVE
- return total
+ return sum(AgenticLoopPipeline._request_message_tokens(msg) for msg in messages)
# ---- LLM client ------------------------------------------------------
diff --git a/tests/agents/chat/test_agent_loop.py b/tests/agents/chat/test_agent_loop.py
index bdd1840e4..7bf2bfd1b 100644
--- a/tests/agents/chat/test_agent_loop.py
+++ b/tests/agents/chat/test_agent_loop.py
@@ -440,6 +440,46 @@ async def test_rejected_forced_choice_keeps_schemas_for_all_later_rounds(monkeyp
assert context.runtime.model_turn["tools"] == attempts[-1]["tools"]
+async def test_context_guard_is_told_which_tool_schemas_each_request_carries(monkeypatch):
+ client = _ScriptedChatClient(
+ [
+ [
+ _llm_chunk(
+ tool_calls=[{"id": "a", "name": "web_search", "arguments": '{"query":"test"}'}],
+ finish_reason="tool_calls",
+ )
+ ],
+ [_llm_chunk(content="Answer", finish_reason="stop")],
+ ]
+ )
+ accepted_create = client.chat.completions.create
+ sent_tools = []
+
+ async def create(**kwargs):
+ sent_tools.append(kwargs.get("tools") or None)
+ return await accepted_create(**kwargs)
+
+ client.chat.completions.create = create
+ pipeline = AgenticChatPipeline(language="en", initial_tool_choice="web_search")
+ pipeline.registry = _Registry()
+ monkeypatch.setattr(pipeline, "_compose_enabled_tools", lambda _: ["web_search"])
+ monkeypatch.setattr(pipeline, "_build_openai_client", lambda: client)
+ guard = pipeline._guard_context_window
+ guarded_tools = []
+
+ async def recording_guard(messages, stream, **kwargs):
+ guarded_tools.append(kwargs.get("tool_schemas") or None)
+ await guard(messages, stream, **kwargs)
+
+ monkeypatch.setattr(pipeline, "_guard_context_window", recording_guard)
+ context = UnifiedContext(session_id="s", user_message="Search")
+ events = await _run(pipeline, context)
+ assert _answer_text(events) == "Answer"
+ assert sent_tools[0]
+ assert guarded_tools == sent_tools
+
+
@pytest.mark.asyncio
async def test_persisted_tool_turn_extends_request_prefix_after_resume(monkeypatch, tmp_path):
"""A new pipeline/database read must preserve seed, reasoning and tools."""
diff --git a/tests/agents/chat/test_context_window_guard.py b/tests/agents/chat/test_context_window_guard.py
new file mode 100644
index 000000000..7f8cf2a25
--- /dev/null
+++ b/tests/agents/chat/test_context_window_guard.py
@@ -0,0 +1,134 @@
+from __future__ import annotations
+
+import copy
+import dataclasses
+import json
+import logging
+from typing import Any
+
+import pytest
+
+from deeptutor.agents.loop.pipeline import CONTEXT_WINDOW_GUARD_RATIO, AgenticLoopPipeline
+from deeptutor.runtime.stream_bus import StreamBus
+from deeptutor.services.session.context_builder import _count_model_context_tokens, count_tokens
+
+WINDOW = 8192
+BUDGET = int(WINDOW * CONTEXT_WINDOW_GUARD_RATIO)
+
+
+def _text(tokens: int) -> str:
+ """Prose that costs about ``tokens`` under the counter the guard uses."""
+ unit = "alpha beta gamma delta "
+ per_unit = count_tokens(unit * 100) / 100 # repeated units merge tokens at the seams
+ return unit * max(1, round(tokens / per_unit))
+
+
+def _pipeline() -> AgenticLoopPipeline:
+ pipeline = AgenticLoopPipeline(language="en")
+ pipeline.llm_config = dataclasses.replace(pipeline.llm_config, context_window=WINDOW)
+ return pipeline
+
+
+def _exec_round(index: int, *, arguments_tokens: int, result_tokens: int) -> list[dict[str, Any]]:
+ call_id = f"call-{index}"
+ arguments = json.dumps({"command": _text(arguments_tokens)})
+ return [
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [
+ {
+ "id": call_id,
+ "type": "function",
+ "function": {"name": "exec", "arguments": arguments},
+ }
+ ],
+ },
+ {"role": "tool", "tool_call_id": call_id, "content": _text(result_tokens)},
+ ]
+
+
+def _snipped(pipeline: AgenticLoopPipeline, messages: list[dict[str, Any]]) -> list[bool]:
+ marker = pipeline._tool_result_snip_marker()
+ return [msg["content"] == marker for msg in messages if msg["role"] == "tool"]
+
+
+async def test_guard_counts_tool_call_arguments_and_trims_oldest_results_first() -> None:
+ pipeline = _pipeline()
+ messages: list[dict[str, Any]] = [{"role": "user", "content": "Plot the projectile ranges."}]
+ for index in range(3):
+ messages += _exec_round(index, arguments_tokens=1400, result_tokens=2000)
+ arguments_before = [msg["tool_calls"] for msg in messages if msg["role"] == "assistant"]
+
+ await pipeline._guard_context_window(messages, StreamBus())
+
+ assert _snipped(pipeline, messages) == [True, True, False]
+ assert [msg["tool_calls"] for msg in messages if msg["role"] == "assistant"] == arguments_before
+ assert pipeline._estimate_messages_tokens(messages) <= BUDGET
+
+
+async def test_guard_counts_the_tool_schemas_the_request_carries() -> None:
+ pipeline = _pipeline()
+ messages: list[dict[str, Any]] = [{"role": "user", "content": "Summarise the figures."}]
+ for index in range(2):
+ messages += _exec_round(index, arguments_tokens=10, result_tokens=1500)
+ schemas = [
+ {
+ "type": "function",
+ "function": {
+ "name": f"tool_{index}",
+ "description": _text(1000),
+ "parameters": {"type": "object", "properties": {}},
+ },
+ }
+ for index in range(6)
+ ]
+
+ await pipeline._guard_context_window(messages, StreamBus(), tool_schemas=schemas)
+
+ assert _snipped(pipeline, messages) == [True, True]
+ fitted = pipeline._estimate_messages_tokens(messages) + _count_model_context_tokens(schemas)
+ assert fitted <= BUDGET
+
+
+def test_estimate_counts_tool_call_arguments_like_history_planning() -> None:
+ messages = _exec_round(0, arguments_tokens=1500, result_tokens=20)
+ arguments = messages[0]["tool_calls"][0]["function"]["arguments"]
+
+ estimate = AgenticLoopPipeline._estimate_messages_tokens(messages)
+
+ assert estimate >= count_tokens(arguments)
+ assert estimate == sum(_count_model_context_tokens(msg) for msg in messages)
+
+
+async def test_guard_warns_and_proceeds_when_nothing_is_left_to_trim(caplog) -> None:
+ pipeline = _pipeline()
+ messages: list[dict[str, Any]] = [{"role": "user", "content": _text(BUDGET + 1000)}]
+ before = copy.deepcopy(messages)
+ caplog.clear()
+
+ with caplog.at_level(logging.WARNING, logger="deeptutor.agents.loop.pipeline"):
+ await pipeline._guard_context_window(messages, StreamBus())
+
+ assert messages == before
+ assert any(
+ "no tool results are left to trim" in record.getMessage() for record in caplog.records
+ )
+
+
+async def test_guard_leaves_a_request_that_fits_alone(caplog) -> None:
+ pipeline = _pipeline()
+ messages: list[dict[str, Any]] = [{"role": "user", "content": "Hi"}]
+ messages += _exec_round(0, arguments_tokens=50, result_tokens=50)
+ before = copy.deepcopy(messages)
+ caplog.clear()
+
+ with caplog.at_level(logging.WARNING, logger="deeptutor.agents.loop.pipeline"):
+ await pipeline._guard_context_window(messages, StreamBus())
+
+ assert messages == before
+ assert not caplog.records