Skip to content

Token usage always shows 0 when using @ai-sdk/openai with custom baseURL (Responses API) #20885

@xialeistudio

Description

@xialeistudio

Description

When using @ai-sdk/openai with a custom baseURL pointing to an OpenAI-compatible proxy, the status bar always displays 0% used and $0.00 spent. Token usage is never tracked despite the upstream API returning valid usage data.

Configuration

{
  "provider": {
    "my-provider": {
      "npm": "@ai-sdk/openai",
      "options": {
        "baseURL": "https://my-proxy.example.com/v1"
      },
      "models": {
        "gpt-5.4": {
          "name": "gpt-5.4"
        }
      }
    }
  }
}

Steps to Reproduce

  1. Configure @ai-sdk/openai with a custom baseURL
  2. Start a conversation with any model
  3. Observe the status bar — token usage and cost remain at 0

Verification

I verified the upstream API returns valid usage data in both non-streaming and streaming modes.

Non-streaming response (excerpt)

{
  "usage": {
    "input_tokens": 18,
    "input_tokens_details": { "cached_tokens": 0 },
    "output_tokens": 13,
    "output_tokens_details": { "reasoning_tokens": 0 },
    "total_tokens": 31
  }
}

Streaming — response.completed event (excerpt)

{
  "type": "response.completed",
  "response": {
    "status": "completed",
    "usage": {
      "input_tokens": 18,
      "input_tokens_details": { "cached_tokens": 0 },
      "output_tokens": 13,
      "output_tokens_details": { "reasoning_tokens": 0 },
      "total_tokens": 31
    }
  }
}

Both were tested via Postman against the same proxy endpoint. The usage field is present and correctly populated.

Expected Behavior

OpenCode should parse the usage field from the response.completed SSE event (streaming) or the response body (non-streaming) and reflect it in the status bar.

Actual Behavior

Status bar shows 0% used / $0.00 spent regardless of actual token consumption. Auto-compaction based on context usage may also be affected.

Notes

  • This is specific to @ai-sdk/openai (Responses API, /v1/responses). The existing fix for @ai-sdk/openai-compatible (includeUsage: true, see Context usage count is always 0 for some local openai-compatible providers #423) does not apply here, as the Responses API uses a different streaming format (response.completed event vs stream_options).
  • The proxy correctly follows the OpenAI Responses API spec.

Environment

  • OS: macOS
  • OpenCode: latest

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions