Skip to content

Bug: OpenCode provider-reported costs are lost, and large executions may have missing detail records #3781

Done

Description

@czehentner

Description

ProPR does not correctly persist LLM costs when using OpenCode with a custom OpenAI-compatible provider through a LiteLLM proxy.

OpenCode emits provider-reported costs in its JSONL output, including part.cost on step_finish events. However, these costs appear to be discarded during parsing and execution-result propagation.

Consequently:

  • llm_executions.cost_usd is recorded as 0.
  • llm_logs.cost_usd is recorded as NULL.
  • The ProPR UI reports $0.00 for executions that incurred real API costs.
  • Cost accounting is unreliable for custom provider/model identifiers that aren't present in ProPR's pricing catalog.

Additionally, a long-running execution was observed with a successfully persisted execution summary but no associated llm_execution_details records.

Environment and deployment setup

The following describes the affected self-hosted setup, with private infrastructure identifiers removed.

Component Configuration
ProPR 0.9.3, self hosted
OpenCode 1.19.6 (agent container)
Agent execution OpenCode running inside Docker containers managed by ProPR
Deployment Docker Compose on an Ubuntu Linux VPS
LiteLLM Self-hosted LiteLLM Proxy, running as a separate service
Upstream model provider External inference API
Model DeepSeek V4.1 Flash 552B
Model configuration Custom OpenAI-compatible provider through LiteLLM
Database SQLite, WAL mode
Git integration GitHub repositories, issues and pull requests

LLM request architecture

ProPR
  |
  | Creates and manages coding-agent execution
  v
OpenCode 1.19.6
  |
  | OpenAI-compatible API
  v
Self-hosted LiteLLM Proxy
  |
  | Routes requests to upstream inference provider
  v
DeepSeek V4.1 Flash 552B
  |
  | Streaming responses
  v
OpenCode JSONL events
  |
  | Usage + step_finish.part.cost
  v
ProPR metrics and logging
  |
  +--> SQLite: llm_executions
  +--> SQLite: llm_execution_details
  +--> SQLite: llm_logs

Example model identifier

The agent uses a custom provider/model identifier resembling:

opencode-litellm/custom-provider/deepseek-v4.1-flash

This is not necessarily a model ID known to ProPR's pricing catalog.

The upstream model is accessed through LiteLLM using an OpenAI-compatible API endpoint. LiteLLM manages routing, provider credentials and model aliases.

Observed proxy performance

An example request recorded by LiteLLM:

Metric Value
Prompt tokens 49,859
Completion tokens 836
Total duration 26.671 s
Time to first token 7.590 s
LiteLLM overhead 8 ms
Provider-reported request cost $0.00094875

LiteLLM therefore has visibility into actual nonzero API costs, while ProPR records zero or missing costs for completed agent executions.

These figures represent one request, not the complete execution described below.

Steps to reproduce

  1. Deploy ProPR using Docker Compose with SQLite as its database.
  2. Configure an OpenCode agent using a custom LiteLLM/OpenAI-compatible provider.
  3. Select a model whose custom identifier is not necessarily recognized by ProPR's pricing catalog.
  4. Run a coding task involving multiple model inference steps.
  5. Confirm that OpenCode emits step_finish events with part.cost.
  6. Allow the execution to complete successfully.
  7. Inspect the execution metrics:
SELECT
    execution_id,
    model_name,
    input_tokens,
    cache_read_input_tokens,
    output_tokens,
    cost_usd
FROM llm_executions
ORDER BY execution_id DESC
LIMIT 10;
  1. Inspect the corresponding LLM log records:
SELECT
    log_id,
    session_id,
    model_name,
    input_tokens,
    cache_read_input_tokens,
    output_tokens,
    cost_usd
FROM llm_logs
ORDER BY log_id DESC
LIMIT 10;
  1. Inspect persisted execution details:
SELECT
    execution_id,
    COUNT(*) AS detail_count
FROM llm_execution_details
GROUP BY execution_id
ORDER BY execution_id DESC
LIMIT 10;

Actual behavior

An observed successful execution produced:

Metric llm_executions llm_logs
Duration 5,983 seconds 5,983 seconds
Input tokens 33,508,527 315,567
Cached input tokens 33,192,960 Recorded separately
Output tokens 144,085 144,085
Cost $0.00 NULL
Persisted conversation details 0 N/A

The difference between the input-token fields is consistent:

33,508,527 total input tokens
-33,192,960 cache-read tokens
--------------------------------
   315,567 uncached input tokens

The token accounting therefore appears internally consistent.

However, the actual provider cost is not correctly reflected in either database table.

For comparison, a shorter execution persisted 50 conversation-detail records successfully.

Expected behavior

  1. Actual costs reported by OpenCode should be retained and persisted.
  2. llm_executions.cost_usd and llm_logs.cost_usd should reflect the actual execution cost.
  3. Provider-reported costs should take precedence over catalog-based estimates when available.
  4. Catalog-based estimation should remain available when actual costs are missing.
  5. Missing cost information should not silently be represented as a measured zero.
  6. Large executions should persist their conversation details reliably, or explicitly report a persistence failure.

Suspected root cause

1. OpenCode costs are not propagated

Relevant source files:

  • packages/core/src/agents/impl/openCodeParsing.ts
  • packages/core/src/agents/impl/OpenCodeAgent.ts
  • packages/core/src/agents/types.ts
  • packages/core/src/utils/llmLogger.ts
  • packages/core/src/utils/llmMetrics.ts
  • src/jobs/issueJob/agent.ts

The apparent data flow is:

OpenCode JSONL
  |
  +-- step_finish.part.tokens
  |      -> parsed successfully
  |
  +-- step_finish.part.cost
         -> not propagated
               |
               v
        AgentExecutionResult
               |
               v
        toClaudeResult()
               |
               v
        recordLLMMetrics()
               |
               v
        Pricing-catalog fallback
               |
               v
        Custom model pricing unavailable
               |
               +--> llm_executions.cost_usd = 0
               +--> llm_logs.cost_usd = NULL

The agent result type already supports an optional cost field, but the OpenCode parsing and persistence paths do not appear to propagate it consistently.

2. Large conversation logs may fail during database persistence

In packages/core/src/utils/llmMetrics.ts, persistToDatabase() inserts the execution summary before processConversationLog() inserts the detail records.

The conversation details are generated and then inserted together:

const detailsArray = claudeResult.conversationLog.map(
  (step, index) =>
    buildConversationDetail({
      step,
      index,
      executionId,
      conversationLog: claudeResult.conversationLog!,
      totalTokens,
      costUsd,
    })
);

await db('llm_execution_details').insert(detailsArray);

For sufficiently large conversation logs, this insertion may exceed SQLite statement or parameter limits.

Because the execution summary is inserted first and persistence errors are caught, an execution can potentially remain recorded even when its details fail to persist.

This second root cause is a hypothesis and requires confirmation through application logs or a reproducible SQLite test.

Suggested fix

  1. Extract provider-reported costs from OpenCode JSONL events.
  2. Correctly aggregate incremental costs without double-counting repeated or cumulative events.
  3. Propagate costs through AgentExecutionResult and the relevant adapters.
  4. Persist actual costs consistently in both metrics tables.
  5. Preserve catalog-based fallback pricing.
  6. Investigate batching or transactional persistence for large conversation logs.
  7. Add regression tests for custom model identifiers and large execution histories.

Suggested regression tests

  • Multiple step_finish events with nonzero costs.
  • Duplicate events and cumulative cost snapshots.
  • Missing cost versus explicitly reported zero cost.
  • Custom LiteLLM provider without catalog pricing.
  • Consistency between llm_executions and llm_logs.
  • Conversation logs containing 500 and 5,000 events.
  • SQLite insertion failures and retries.
  • Preservation of existing cached-token accounting.

Impact

This affects observability and cost reporting for users running OpenCode with custom or self-hosted OpenAI-compatible providers.

For long-running coding agents, executions can involve millions of cumulative cached tokens and substantial actual API costs while appearing to cost $0.00 in ProPR.

The potentially related execution-detail issue also prevents effective debugging of excessive agent iterations, latency and token consumption.

Activity

  1. roy-tong commented on Nov 27, 2026

    @roy-tong

    A concrete next check: Bug: OpenCode provider-reported costs are lost, and large executions may have missing detail records. Record the provider-billed request, retry count and accepted result separately; a passing check is not an accepted outcome. Can you share one reproduction?

  2. integry commented on Dec 1, 2026

    @integry
    Owner

    Fixed by #3780 (provider-reported OpenCode costs and execution detail records), with the partial-step cost follow-up in #3961 (#3960). Both are on main and ship in 0.9.4.

  3. roy-tong commented on Dec 1, 2026

    @roy-tong

    The two failure modes should be separated in the schema: provider-reported cost known, unknown, and catalog-estimated. Storing unknown as 0 makes a later aggregate indistinguishable from a genuinely free call. I would persist the OpenCode step_finish cost sum with a basis field, keep catalog estimates only when reported cost is absent, and make missing detail persistence fail the execution record rather than leave a summary-only success.

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