Skip to content

fix(logs): logging with error issues for model costs - #2169

Merged
icecrasher321 merged 4 commits into
stagingfrom
fix/execution-bugs
Dec 3, 2025
Merged

fix(logs): logging with error issues for model costs#2169
icecrasher321 merged 4 commits into
stagingfrom
fix/execution-bugs

Conversation

@icecrasher321

Copy link
Copy Markdown
Collaborator

Summary

Async Executions got removed in new executor

Schedules, Webhooks, API executions are not tracking LLM cost when there's a downstream failure

Schedules execution errors — always surface as workflow error without trace psan

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Other: ___________

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercelBot commented Dec 3, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
ProjectDeploymentPreviewCommentsUpdated (UTC)
docsSkippedSkippedDec 3, 2025 4:50am

@icecrasher321icecrasher321 changed the title fix(fix(logs): logging with error issues for model costsDec 3, 2025
@greptile-apps

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

Restores async execution support and fixes critical cost tracking issues for scheduled workflows.

  • Async Execution: Re-added handleAsyncExecution function to queue workflows via Trigger.dev when X-Execution-Mode: async header is set
  • Schedule Error Tracking: Fixed schedule executions to collect trace spans even on failure by extracting executionResult from errors and calling buildTraceSpans before logging
  • Cost Calculation on Error: Modified completeWithError to calculate cost summary from provided trace spans instead of defaulting to zero cost, ensuring LLM costs are tracked when workflows fail downstream
  • Token Field Compatibility: Updated token counting to handle both new (input/output) and legacy (prompt/completion) field names using nullish coalescing operators

The changes ensure proper cost tracking for failed executions and restore the ability to run workflows asynchronously for API, webhook, and schedule triggers.

Confidence Score: 5/5

  • This PR is safe to merge with no blocking issues identified
  • All changes are bug fixes that restore missing functionality and fix critical cost tracking issues. The implementation properly handles edge cases, uses defensive programming with nullish coalescing, and follows established patterns from the codebase
  • No files require special attention

Important Files Changed

File Analysis

FilenameScoreOverview
apps/sim/app/api/workflows/[id]/execute/route.ts5/5Restored async execution functionality by adding handleAsyncExecution function and checking X-Execution-Mode header
apps/sim/background/schedule-execution.ts5/5Fixed trace span collection on schedule failures by extracting executionResult and building trace spans before logging errors
apps/sim/lib/logs/execution/logging-factory.ts5/5Fixed token counting to handle both input/output and legacy prompt/completion token field names using nullish coalescing
apps/sim/lib/logs/execution/logging-session.ts5/5Fixed cost tracking when workflows error by calculating cost summary from provided trace spans instead of always using zero cost

Sequence Diagram

sequenceDiagram
participant Client
participant API as Workflow Execute API
participant TriggerDev as Trigger.dev
participant Executor as Workflow Executor
participant Logger as Logging Session
participant TraceSpans as Trace Spans Builder
participant DB as Database
Note over Client,DB: Async Execution Flow (Restored)
Client->>API: POST /api/workflows/[id]/execute<br/>X-Execution-Mode: async
API->>API: Check isAsyncMode header
API->>TriggerDev: tasks.trigger('workflow-execution')
TriggerDev-->>API: Return handle with jobId
API-->>Client: 202 Accepted with jobId
Note over Client,DB: Schedule Execution with Error
TriggerDev->>Executor: Execute scheduled workflow
Executor->>Logger: safeStart()
Executor-->>TriggerDev: Execution fails with earlyError
TriggerDev->>TraceSpans: buildTraceSpans(executionResult)
TraceSpans-->>TriggerDev: Return traceSpans with cost data
TriggerDev->>Logger: safeCompleteWithError({traceSpans})
Logger->>Logger: calculateCostSummary(traceSpans)
Logger->>DB: Save execution log with costs
Note over Client,DB: Cost Tracking Fix
Logger->>Logger: Check if traceSpans provided
alt Has trace spans
Logger->>Logger: calculateCostSummary(traceSpans)
Note over Logger: Uses input/output or<br/>prompt/completion tokens
else No trace spans
Logger->>Logger: Use BASE_EXECUTION_CHARGE only
end
Logger->>DB: Save with calculated costs
Loading

@greptile-appsgreptile-appsBot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@icecrasher321
icecrasher321 merged commit 7de721e into stagingDec 3, 2025
9 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/execution-bugs branch December 4, 2025 05:14
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@icecrasher321