Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • added audit log for billing line items

Type of Change

  • Bug fix

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
Copy link

vercel bot commented Dec 20, 2025

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

1 Skipped Deployment
Project Deployment Review Updated (UTC)
docs Skipped Skipped Dec 20, 2025 9:41pm

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Dec 20, 2025

Greptile Summary

This PR adds comprehensive audit logging for all billing operations by creating a new usage_log table that tracks individual billing line items across workflows, wand, and copilot usage.

Key Changes:

  • Created usage_log table with proper indexes for efficient querying by user, timestamp, source, workspace, and workflow
  • Implemented three logging functions: logModelUsage() for token-based charges, logFixedUsage() for flat fees, and logWorkflowUsageBatch() for performance-optimized batch inserts
  • Integrated audit logging into all billing touchpoints: workflow executions, wand operations, and copilot API calls
  • Added new GET endpoint /api/users/me/usage-logs with filtering and cursor-based pagination
  • Cursor pagination correctly handles duplicate timestamps using compound ordering on (createdAt, id)
  • Error handling ensures audit logging failures don't break main billing flow

Implementation Quality:

  • Uses batch inserts for workflow executions to minimize database overhead
  • Proper foreign key constraints with SET NULL on deletion for optional references
  • Non-blocking error handling - audit failures are logged but don't throw
  • Comprehensive indexes support common query patterns

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The implementation is well-architected with proper error handling, database indexes, and pagination logic. The previous cursor pagination issue has been fixed with compound ordering. All logging functions are non-blocking and won't break billing operations if they fail. The schema design follows best practices with appropriate foreign key constraints and indexes.
  • No files require special attention

Important Files Changed

Filename Overview
packages/db/schema.ts defines usage_log table schema with proper indexes on user_id, created_at, source, workspace_id, and workflow_id
apps/sim/lib/billing/core/usage-log.ts implements audit logging functions with proper error handling, cursor pagination (with compound ordering), and batch inserts for performance
apps/sim/app/api/billing/update-cost/route.ts adds model, inputTokens, outputTokens fields to schema and calls logModelUsage for copilot usage tracking
apps/sim/app/api/wand/route.ts fetches workspaceId from workflow and calls logModelUsage to track wand usage with proper context
apps/sim/lib/logs/execution/logger.ts adds executionId parameter throughout and calls logWorkflowUsageBatch with base execution charge and per-model costs
apps/sim/app/api/users/me/usage-logs/route.ts new GET endpoint for users to retrieve their usage logs with filtering by source, workspace, period, and cursor pagination

Sequence Diagram

sequenceDiagram
    participant Client
    participant API as API Endpoint
    participant Logger as Usage Logger
    participant UserStats as User Stats Table
    participant UsageLog as Usage Log Table
    participant Billing as Billing Service

    Note over Client,Billing: Workflow Execution Flow
    Client->>API: Execute Workflow
    API->>Logger: ExecutionLogger.updateUserStats()
    Logger->>UserStats: Update aggregate stats (cost, tokens)
    Logger->>Logger: logWorkflowUsageBatch()
    Logger->>UsageLog: Batch insert (execution fee + per-model entries)
    Logger->>Billing: checkAndBillOverageThreshold()
    API-->>Client: Execution complete

    Note over Client,Billing: Wand Usage Flow
    Client->>API: POST /api/wand
    API->>UserStats: Update aggregate wand stats
    API->>Logger: logModelUsage(source='wand')
    Logger->>UsageLog: Insert single model usage entry
    API->>Billing: checkAndBillOverageThreshold()
    API-->>Client: Wand response

    Note over Client,Billing: Copilot Usage Flow
    Client->>API: POST /api/billing/update-cost
    API->>UserStats: Update aggregate copilot stats
    API->>Logger: logModelUsage(source='copilot')
    Logger->>UsageLog: Insert single model usage entry
    API->>Billing: checkAndBillOverageThreshold()
    API-->>Client: Cost update confirmed

    Note over Client,Billing: Query Usage Logs Flow
    Client->>API: GET /api/users/me/usage-logs
    API->>Logger: getUserUsageLogs()
    Logger->>UsageLog: Query with filters (source, workspace, date range)
    UsageLog-->>Logger: Paginated logs + summary
    Logger-->>API: Usage logs result
    API-->>Client: Return logs with pagination
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

8 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@waleedlatif1
Copy link
Collaborator Author

@greptile

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

8 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@waleedlatif1
Copy link
Collaborator Author

@greptile

@waleedlatif1 waleedlatif1 merged commit 35a57bf into staging Dec 20, 2025
11 checks passed
@waleedlatif1 waleedlatif1 deleted the feat/audit branch December 20, 2025 22:10
Sign up for free to 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.

2 participants