Skip to content

Context usage count is always 0 for some local openai-compatible providers #423

@unkhz

Description

@unkhz

The openai-compatible models work nicely, but token counts are not calculated correctly, which may cause other issues. E.g. the auto summarization of the conversation will not happen when token limits are approaching.

Problem occurs at least with these local LLM servers:

  • LM studio on Mac
  • Ollama on Windows

Problem does NOT occur with

  • llama.cpp

Below an example response from LM studio. The tokens are sent by LM studio, but they appear to be NaN when debug logging the response.

Not sure if this is ai-sdk or opencode issue.

Config

{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "lmstudio": {
      "npm": "@ai-sdk/openai-compatible",
      "options": {
        "baseURL": "http://localhost:8080/v1"
      },
      "models": {
        "qwen/qwen3-30b-a3b": {
          "limit": { "context": 131072, "output": 4096 }
        },
        "mistralai/devstral-small-2505": {
          "limit": { "context": 131072, "output": 4096 }
        }
      }
    }
  }
}

Usage coming from ai-sdk

Image

Raw query towards LM studio endpoint

$ curl -X POST "http://localhost:8080/v1/chat/completions" \
  -H "Content-Type: application/json" \
  -d '{"model":"qwen/qwen3-30b-a3b","messages":[{"role":"user","content":"hello"}]}'

Raw output from LM studio endpoint

{
  "id": "chatcmpl-nziofmbwmwbf87ynkt0bn",
  "object": "chat.completion",
  "created": 1750951509,
  "model": "qwen/qwen3-30b-a3b",
  "choices": [
    {
      "index": 0,
      "logprobs": null,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "<think>\nOkay, the user sent \"hello\". I need to respond appropriately. Since it's a greeting, the best approach is to greet them back and offer help. I should keep it friendly and open-ended. Let me make sure the response is welcoming and encourages them to ask questions. Maybe something like, \"Hello! How can I assist you today?\" That should work.\n</think>\n\nHello! How can I assist you today? 😊"
      }
    }
  ],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 89,
    "total_tokens": 98
  },
  "stats": {},
  "system_fingerprint": "qwen/qwen3-30b-a3b"
}

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