Skip to content

symfonic.capabilities.prompting.budgeting

budgeting

Step 6, as the compiled value reports it: admission under the budget.

Split out of :mod:.compiler for the 300-line module budget, and the split lands here rather than in :mod:.budget because of the import direction: :mod:.request imports :mod:.budget, so the arithmetic that needs a PromptCompileRequest cannot live there without a cycle. :mod:.budget owns the rule -- the drop order and what pinned means -- and this module owns the one call that applies it to a compile and records what it cost.

apply_budget

apply_budget(request: PromptCompileRequest, rendered: tuple[RenderedContribution, ...], diagnostics: list[PromptDiagnostic]) -> tuple[RenderedContribution, ...]

Step 6: admission under the budget, in the fixed drop order.

Source code in src/symfonic/capabilities/prompting/budgeting.py
def apply_budget(
    request: PromptCompileRequest,
    rendered: tuple[RenderedContribution, ...],
    diagnostics: list[PromptDiagnostic],
) -> tuple[RenderedContribution, ...]:
    """Step 6: admission under the budget, in the fixed drop order."""
    by_id = {row.contribution_id: row for row in rendered}
    rows = [
        BudgetRow(
            contribution_id=row.contribution_id,
            layer=row.layer,
            order=index,
            tokens=row.tokens,
            pinned=row.pinned,
        )
        for index, row in enumerate(rendered)
    ]
    admitted, report = admit_within_budget(rows, request.budget)
    for dropped in report.dropped:
        diagnostics.append(
            PromptDiagnostic(
                "budget",
                dropped,
                f"dropped: {by_id[dropped].tokens} tokens would exceed the "
                f"{report.limit}-token budget",
            )
        )
    return tuple(by_id[row.contribution_id] for row in admitted)

budget_report

budget_report(request: PromptCompileRequest, rendered: tuple[RenderedContribution, ...], survivors: tuple[RenderedContribution, ...]) -> BudgetReport

The budget arithmetic as the compiled value reports it.

Source code in src/symfonic/capabilities/prompting/budgeting.py
def budget_report(
    request: PromptCompileRequest,
    rendered: tuple[RenderedContribution, ...],
    survivors: tuple[RenderedContribution, ...],
) -> BudgetReport:
    """The budget arithmetic as the compiled value reports it."""
    survivor_ids = {row.contribution_id for row in survivors}
    return BudgetReport(
        limit=request.budget.max_total_tokens,
        total_tokens=sum(row.tokens for row in survivors),
        admitted=tuple(row.contribution_id for row in survivors),
        dropped=tuple(
            row.contribution_id for row in rendered if row.contribution_id not in survivor_ids
        ),
    )