Skip to content

Record and report spend on failed and errored attempts #3437

Closed

Description

@integry

Problem

Spend on failed attempts cannot be reported. Three defects: (1) a Claude run that hits a usage limit throws before execution logs are persisted, so it writes no llm_executions or llm_logs row; agent catch paths write no row either; (2) when a step after the agent run fails, the error handler calls recordLLMMetrics a second time for the same result, so the execution can be counted twice; (3) the analytics aggregates sum all executions regardless of success.

Goal

Every agent execution that consumed tokens has exactly one execution row, and analytics can split spend by outcome.

Acceptance criteria

  • Executions that end in a usage limit or a thrown agent error record one llm_executions row with success = false, the error message, and whatever token and cost figures were observed in the stream (zero when none were).
  • recordLLMMetrics is idempotent per execution: a second call for the same execution id does not insert a second row. Covered for both the issue path and the PR-comment path.
  • loadRecordedSpend and loadModelUsage return spend split into successful and failed executions; totals are unchanged in meaning. The stats/overview and dashboard responses expose the split as additional fields (no existing field is renamed or removed).
  • The Analytics page shows "spend on failed attempts" for the selected window.
  • Redis llm:metrics:* counters stay consistent with the rows (no double increment).
  • Tests cover the usage-limit path, the thrown-error path, the idempotency guard, and the aggregate split.

Implementation

Key files: packages/core/src/agents/impl/utils/dockerResultProcessor.ts (usage limit thrown before logging), packages/core/src/agents/impl/ClaudeAgent.ts (persistExecutionLogs, catch path), packages/core/src/agents/impl/CodexAgent.ts, packages/core/src/utils/llmMetrics.ts (recordLLMMetrics, persistToDatabase), double-call sites src/jobs/processPullRequestCommentJob.ts + src/jobs/prCommentJobUtils.ts and src/jobs/processGitHubIssueJob.ts + src/jobs/errorHandlers.ts, packages/api/routes/analyticsAggregates.ts, packages/api/routes/analyticsModelUsage.ts, packages/api/routes/statsRoutes.ts, propr-ui/src/pages/AnalyticsPage.tsx. Tests: test/llmMetrics*.test.ts, test/claudeRateLimitPropagation.test.ts, packages/api/test/analyticsAggregates.test.ts. Verify with the standard commands (test:prepare, typecheck, changed tests, eslint --max-warnings 0 per package).

Activity

  1. propr-dev commented on Nov 11, 2026

    @propr-dev
    Contributor

    🤖 AI processing has started for this issue using claude agent with claude-opus-5-5 model.

    I'll analyze the problem and work on a solution. This may take a few minutes.

    Processing Details:

    • Agent: claude
    • Model: claude-opus-5-5
    • Branch: 3437/claude-opus-5-5-record-and-report-spend-o-20261111-2200-41r
    • Base Branch: 3437-epic-cost-visibility-olu
    • Worktree: issue-3437-20261111-2200-claude-opus-5-5-41r

    🔍 Track Task Execution

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions