Skip to content

latent.context.budget

Budget breakdown utilities for LLM message arrays.

Classes

BudgetBreakdown

BudgetBreakdown(total_tokens: int, system_tokens: int, history_tokens: int, tool_def_tokens: int, tool_output_tokens: int, available_tokens: int, utilization: float, components: list[dict[str, Any]] = list())

Per-component token allocation derived from a messages array.

Functions

budget_breakdown

budget_breakdown(messages: list[dict[str, Any]], tools: list[dict[str, Any]] | None = None, model_limit: int = 200000) -> BudgetBreakdown

Compute per-component token allocation from a messages array.

Categorises messages by role:

  • system -- messages with role="system"
  • tool output -- messages with role="tool"
  • history -- everything else (user, assistant, function, etc.)

Tool definitions are estimated by serialising the tools list to JSON.

Parameters

messages: An OpenAI-style list of message dicts (each must have "role"). tools: Optional list of tool/function definition dicts. model_limit: Context window size in tokens.

Returns

BudgetBreakdown