openclaw - 💡(How to fix) Fix Context token count shows 0 for Ollama provider despite usage data being returned [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#73832Fetched 2026-04-29 06:14:29
View on GitHub
Comments
1
Participants
2
Timeline
2
Reactions
0
Author
Timeline (top)
closed ×1commented ×1

Root Cause

Ollama's OpenAI-compatible API (http://localhost:11434/v1/chat/completions) does return token usage data in the response:

"usage": { "prompt_tokens": 10, "completion_tokens": 10, "total_tokens": 20 }

However, OpenClaw's Ollama provider integration doesn't read/parse the usage field from Ollama's response, so the context display always shows 0.

Code Example

"usage": { "prompt_tokens": 10, "completion_tokens": 10, "total_tokens": 20 }

---

"ollama": {
  "baseUrl": "http://localhost:11434/v1",
  "api": "openai-completions",
  "apiKey": "ollama",
  "models": [{
    "id": "phi3.5:latest",
    "name": "Phi-3.5 (Local GPU)",
    "contextWindow": 4096,
    "maxTokens": 2048,
    "input": ["text"],
    "cost": { "input": 0, "output": 0 }
  }]
}
RAW_BUFFERClick to expand / collapse

Bug Description

When using the Ollama provider (ollama/phi3.5:latest), the session status shows 0/197k tokens (0%) even after active conversation. The context counter doesn't track token usage for Ollama models.

Root Cause

Ollama's OpenAI-compatible API (http://localhost:11434/v1/chat/completions) does return token usage data in the response:

"usage": { "prompt_tokens": 10, "completion_tokens": 10, "total_tokens": 20 }

However, OpenClaw's Ollama provider integration doesn't read/parse the usage field from Ollama's response, so the context display always shows 0.

Expected Behavior

Token usage should be tracked accurately for Ollama models. The model config correctly specifies contextWindow: 4096 for phi3.5, but the display falls back to the fallback model's 196K context.

Environment

  • OpenClaw version: 2026.4.23
  • Ollama version: running locally on http://localhost:11434
  • Model: phi3.5:latest (3.8B, Q4_0, 4K context)
  • GPU: NVIDIA 960M (4GB)

Model Config

"ollama": {
  "baseUrl": "http://localhost:11434/v1",
  "api": "openai-completions",
  "apiKey": "ollama",
  "models": [{
    "id": "phi3.5:latest",
    "name": "Phi-3.5 (Local GPU)",
    "contextWindow": 4096,
    "maxTokens": 2048,
    "input": ["text"],
    "cost": { "input": 0, "output": 0 }
  }]
}

Steps to Reproduce

  1. Configure Ollama provider in openclaw.json
  2. Switch to Ollama model via /model ollama/phi3.5:latest
  3. Have an active conversation
  4. Run /status — context shows 0 tokens despite active conversation

Suggested Fix

Ensure the Ollama provider parses the usage field from Ollama's API response. The data is already being returned; it's just not being read.

extent analysis

TL;DR

Update the Ollama provider integration in OpenClaw to parse the usage field from Ollama's API response to accurately track token usage.

Guidance

  • Review the OpenClaw Ollama provider code to identify where the API response is being processed and modify it to extract the usage field.
  • Verify that the usage field is being correctly parsed by logging or debugging the response data.
  • Update the context display logic to use the parsed usage data to show accurate token usage.
  • Test the changes by reproducing the steps to reproduce and checking the context display after an active conversation.

Example

// Example of parsing the usage field from Ollama's API response
const usageData = response.data.usage;
const totalTokens = usageData.total_tokens;
// Update the context display with the total tokens used

Notes

The fix assumes that the Ollama API response format remains consistent and that the usage field is always present. Additional error handling may be necessary to handle cases where the usage field is missing or malformed.

Recommendation

Apply workaround: Update the Ollama provider integration to parse the usage field, as this will allow accurate tracking of token usage without requiring any changes to the Ollama API or OpenClaw core functionality.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix Context token count shows 0 for Ollama provider despite usage data being returned [1 comments, 2 participants]