AgentWorks

AgentWorks documentation

Cost And Log Measurement

This doc explains how workflow costs and logs are measured now.

It is not the same as workflow_monitoring.md.

Two Different Systems

There are two separate data planes:

1. Cost measurement

2. Workflow logs

Costs are not reconstructed from log files. Logs are not reconstructed from cost files.

Cost Measurement Source Of Truth

The source of truth for workflow cost measurement is the token_usage event stream.

Current flow:

  1. An agent emits a token_usage event.
  2. The context-aware bridge intercepts that event.
  3. It extracts:
    • provider
    • model ID
    • prompt/input tokens
    • completion/output tokens
    • cache read and cache write tokens
    • reasoning tokens
    • LLM call count
  4. It persists those values directly to the costs/ ledger.

This happens in context_aware_bridge.go.

What Gets Counted

The cost ledger tracks per-model usage with pricing fields:

The main types are defined in:

Important nuance:

Cost Storage Layout

The current storage layout is under costs/.

Phase-only costs

Used for phase agents such as planning and builder-style workflow-phase sessions.

Files:

These are written by PersistPhaseTokenUsage(...) in base_orchestrator_tokens.go.

Builder workflow-phase sessions also write phase cost data directly in server.go.

Execution run costs

Used for normal workflow execution runs.

Files:

Each daily file stores a map of run folders:

So one daily bucket can contain multiple runs for the same group on the same UTC date.

Evaluation run costs

Used for evaluation execution.

Files:

Evaluation uses the same ledger shape as execution, but under the evaluation scope.

Aggregation Shape

Run-level cost data is aggregated into two main maps:

by_model:

by_step_and_model:

This is what lets the UI render:

Legacy Compatibility

Older layouts still exist in some workspaces:

Current behavior:

This logic is in cost_storage.go and token_usage_store.go.

How /api/workflow/costs Works

The workflow cost API does not read a single per-run JSON file anymore.

Current behavior:

This happens in:

Step Token Usage Events

step_token_usage is a derived summary event, not the primary source of truth.

Current behavior:

This means:

See base_orchestrator_tokens.go.

Workflow Log Source Of Truth

Workflow logs are explicit artifact files written by workflow controllers.

They are not reconstructed from the database event stream.

The main log families are:

Log File Layout

For a normal run folder, logs live under:

Common files:

There are also builder/session logs under:

Who Writes The Logs

Main writers:

Archived Logs

When a step is re-executed, older log files can be archived instead of being discarded.

Current behavior:

This is handled in controller_progress.go.

How /api/workflow/logs Works

The workflow log API scans the run folder and reconstructs a structured view for the UI.

Current behavior:

This is why the execution log popup can show:

The implementation is in workflow.go.

Practical Summary

Use this mental model:

Related Docs