Skip to content

🤖 fix: auto model routing drops billed usage of invalid evaluation answers #4774

Description

@ThomasK33

Problem

autoModelRouter (src/node/services/autoModelRouter.ts, around lines 82-116) calls experimental_evaluate directly. When the evaluation response is rejected as invalid, the router returns an error without usage. agentSession then takes its fallback path, which skips both recordHeadlessUsage and deferEvaluatorGoalCharge. The provider has already billed that classifier request, but it never reaches the workspace ledger or the goal's cost cap.

The cause is the one fixed for workflow and intuition evaluations in #4773 (Refs #4728). @ai-sdk/provider-utils EvaluationLanguageModel validates answers inside doEvaluate and throws InvalidResponseDataError before it returns usage.

Proposed fix

Reuse #4773's capture in the router: the per-call doEvaluate wrapper plus the AsyncLocalStorage scope around the inner doGenerate. Export a small helper from evaluationService.ts if needed. On an invalid-answer failure that carries billed usage, record the headless row with analyticsSource: "auto_model_routing" and apply the deferred goal charge, as the success path does.

Trigger

#4773 merged (the capture helper exists on main). Owner: the issue coordinator.


Generated with xum • Model: anthropic:claude-opus-5-5 • Thinking: high • Cost: $46.76

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions