Skip to content

symfonic.platform.token_composition

token_composition

Estimate the provider input's four dashboard categories.

The provider reports one authoritative input total, not a section breakdown. The transcript is the authoritative shape of what it read; this module weighs its sections and apportions that total so the displayed categories always add back to the provider's number. No prompt or tool argument is retained.

estimate_composition

estimate_composition(plan: Any, transcript: Any, *, input_tokens: int) -> dict[str, int]

Return estimated section counts summing to input_tokens.

Source code in src/symfonic/platform/token_composition.py
def estimate_composition(
    plan: Any, transcript: Any, *, input_tokens: int
) -> dict[str, int]:
    """Return estimated section counts summing to ``input_tokens``."""
    messages = tuple(getattr(transcript, "wire", ()) or ())
    system = ""
    history: list[str] = []
    for message in messages:
        content = _text(getattr(message, "content", ""))
        if getattr(message, "type", "") == "system" and not system:
            system = content
        else:
            history.append(content)

    memory_blocks = _MEMORY_BLOCK.findall(system)
    memory = " ".join(memory_blocks)
    system_without_memory = _MEMORY_BLOCK.sub("", system)
    tools = " ".join(
        f"{tool.name} {tool.description} {dict(tool.policy)}"
        for tool in getattr(getattr(plan, "tool_manifest", None), "tools", ())
    )
    counts = _apportion(
        tuple(map(_weight, (system_without_memory, tools, " ".join(history), memory))),
        input_tokens,
    )
    return {
        "system_prompt_tokens": counts[0],
        "tool_definitions_tokens": counts[1],
        "conversation_history_tokens": counts[2],
        "memory_context_tokens": counts[3],
    }