Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

3.3 KB
Raw Blame
"""Iteration budget: limits per conversation turn.

Two parallel limits:
- `max_turns`: number of allowed tool-use loops (prevents infinite loops). This
  is the hard iteration bound.
- `max_tokens_total`: ceiling on the tokens the loop *generates* (output),
  guarding against runaway generation.

`tokens_used` counts OUTPUT tokens only — NOT the re-sent prompt. The prompt
(system + tool schemas + the full conversation history) is re-sent on every
turn and grows with the conversation; counting it here let a single
large-context turn exhaust the whole loop budget and abort a pending tool call
before execution (live bug 2026-06-01: "Einen Moment." then silence in a long
voice session — an AD-OE6 silent drop). The re-sent prompt is bounded by
`max_turns` × the context window, so `max_turns` is the cost/loop guard; this
ceiling catches genuine runaway *generation*.

Both are *soft hints* — the API may exceed them by up to ~10% mid-callback. We
therefore combine a hard count with a soft check.
"""
from __future__ import annotations

from dataclasses import dataclass


@dataclass
class IterationBudget:
    """Tracks turns and generated tokens for a conversation loop."""
    # Maintainer mandate 2026-08-24: no cost backstop — these are loop guards.
    max_turns: int = 100
    max_tokens_total: int = 2_000_000
    #: Cost backstop over the CUMULATIVE re-sent prompt. Distinct from the
    #: 2026-06-01 trap above: input never counts against the small OUTPUT
    #: ceiling — this is its own bound, sized so no legitimate flow reaches
    #: it (~15 rounds of a 27k-token fixed block). Live 2026-07-26: one
    #: voice turn summed 686k input tokens across loop rounds ≈ $1 on the
    #: configured tool model. Tripping this behaves exactly like the other
    #: two bounds: the loop's budget directive forces one final grounded
    #: synthesis round — an honest answer, never a silent drop.
    max_input_tokens_total: int = 50_000_000
    turns_used: int = 0
    tokens_used: int = 0           # OUTPUT tokens accumulated (runaway-generation signal)
    input_tokens_seen: int = 0     # cumulative re-sent prompt (cost backstop + telemetry)

    def record_turn(self, tokens_in: int = 0, tokens_out: int = 0) -> None:
        self.turns_used += 1
        # Only generated (output) tokens count toward the OUTPUT ceiling — see
        # module docstring. The re-sent prompt accumulates against its own,
        # far larger cost backstop.
        self.tokens_used += int(tokens_out)
        self.input_tokens_seen += int(tokens_in)

    def exceeded(self) -> bool:
        return (
            self.turns_used >= self.max_turns
            or self.tokens_used >= self.max_tokens_total
            or self.input_tokens_seen >= self.max_input_tokens_total
        )

    def remaining_turns(self) -> int:
        return max(0, self.max_turns - self.turns_used)

    def remaining_tokens(self) -> int:
        return max(0, self.max_tokens_total - self.tokens_used)

    def snapshot(self) -> dict[str, int]:
        return {
            "turns_used": self.turns_used,
            "tokens_used": self.tokens_used,            # output tokens
            "turns_remaining": self.remaining_turns(),
            "tokens_remaining": self.remaining_tokens(),
            "input_tokens_seen": self.input_tokens_seen,  # re-sent prompt (telemetry)
        }