openclaw - ✅(Solved) Fix [Bug]: agents.defaults.models.*.params.num_ctx ignored for Ollama — model discovery always overrides with GGUF context_length [2 pull requests, 1 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#44550Fetched 2026-04-08 00:45:21
View on GitHub
Comments
0
Participants
1
Timeline
13
Reactions
0
Author
Participants
Timeline (top)
referenced ×9cross-referenced ×2labeled ×2

OpenClaw ignores user-configured num_ctx in openclaw.json when calling Ollama; on every startup it queries /api/show, reads the GGUF metadata field qwen2.context_length: 32768, and passes that value as num_ctx in every /api/chat request, making params.num_ctx: 8192 in openclaw.json have no effect.

Root Cause

OpenClaw ignores user-configured num_ctx in openclaw.json when calling Ollama; on every startup it queries /api/show, reads the GGUF metadata field qwen2.context_length: 32768, and passes that value as num_ctx in every /api/chat request, making params.num_ctx: 8192 in openclaw.json have no effect.

Fix Action

Fix / Workaround

Workaround: Using anthropic/claude-haiku-4-5 as primary model, with Ollama as fallback.

PR fix notes

PR #44678: ollama: honor explicit params.num_ctx overrides

Description (problem / solution / changelog)

Summary

  • Problem: native Ollama runs always send options.num_ctx from model.contextWindow, so agents.defaults.models["ollama/<model>"].params.num_ctx never takes effect.
  • Why it matters: users explicitly configuring a smaller Ollama context still get the discovered GGUF context length, which can inflate KV cache usage and latency.
  • What changed: native Ollama requests now honor payload mutations, and the extra-param wrapper injects configured num_ctx for native ollama models.
  • What did NOT change (scope boundary): this PR does not change model discovery, stored model metadata, or the OpenAI-compatible Ollama adapter path.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

  • Closes #44550
  • Related #44550

User-visible / Behavior Changes

Native Ollama runs now prefer agents.defaults.models["ollama/<model>"].params.num_ctx over the discovered contextWindow when building /api/chat requests.

Security Impact (required)

  • New permissions/capabilities? (Yes/No) No
  • Secrets/tokens handling changed? (Yes/No) No
  • New/changed network calls? (Yes/No) No
  • Command/tool execution surface changed? (Yes/No) No
  • Data access scope changed? (Yes/No) No
  • If any Yes, explain risk + mitigation:

Repro + Verification

Environment

  • OS: macOS / local dev checkout
  • Runtime/container: Node 22 + Bun/Vitest
  • Model/provider: native Ollama stream path
  • Integration/channel (if any): None
  • Relevant config (redacted): agents.defaults.models["ollama/qwen2.5:14b-8k"].params.num_ctx = 8192

Steps

  1. Configure a native Ollama model in agents.defaults.models with params.num_ctx.
  2. Run the model through the native ollama stream path.
  3. Inspect the generated /api/chat request payload.

Expected

  • options.num_ctx uses the explicit configured override.

Actual

  • The issue report shows Ollama continuing to receive the discovered context length instead of the configured override.

Evidence

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

Human Verification (required)

What you personally verified (not just CI), and how:

  • Verified scenarios:
    • bunx vitest run src/agents/ollama-stream.test.ts src/agents/pi-embedded-runner-extraparams.test.ts
    • pnpm exec oxfmt --check src/agents/ollama-stream.ts src/agents/ollama-stream.test.ts src/agents/pi-embedded-runner/extra-params.ts src/agents/pi-embedded-runner-extraparams.test.ts
    • pnpm exec oxlint src/agents/ollama-stream.ts src/agents/ollama-stream.test.ts src/agents/pi-embedded-runner/extra-params.ts src/agents/pi-embedded-runner-extraparams.test.ts
  • Edge cases checked:
    • Payload hooks can override the default native num_ctx.
    • Existing payload options are preserved when num_ctx is injected.
  • What you did not verify:
    • I did not run a live Ollama daemon or full integration channel flow.

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

If a bot review conversation is addressed by this PR, resolve that conversation yourself. Do not leave bot review conversation cleanup for maintainers.

Compatibility / Migration

  • Backward compatible? (Yes/No) Yes
  • Config/env changes? (Yes/No) No
  • Migration needed? (Yes/No) No
  • If yes, exact upgrade steps:

Failure Recovery (if this breaks)

  • How to disable/revert this change quickly: revert this commit or remove params.num_ctx from the model entry.
  • Files/config to restore: src/agents/ollama-stream.ts, src/agents/pi-embedded-runner/extra-params.ts
  • Known bad symptoms reviewers should watch for: native Ollama requests ignoring explicit num_ctx and falling back to discovered context length again.

Risks and Mitigations

  • Risk: native Ollama payload hooks now run before the request is serialized.
    • Mitigation: the new regression test covers overriding num_ctx through onPayload, and the change is limited to the native Ollama request builder.

Changed files

  • src/agents/ollama-stream.test.ts (modified, +40/-0)
  • src/agents/ollama-stream.ts (modified, +1/-0)
  • src/agents/pi-embedded-runner-extraparams.test.ts (modified, +43/-1)
  • src/agents/pi-embedded-runner/extra-params.ts (modified, +60/-2)

PR #47160: fix: prioritize user-configured num_ctx over model-discovery contextWindow for Ollama (closes #44550)

Description (problem / solution / changelog)

Summary

  • Problem: OpenClaw ignores user-configured num_ctx in openclaw.json when calling Ollama. On every startup it queries /api/show, reads the GGUF metadata context_length (e.g. 32768), and passes that value as num_ctx in every API request, making params.num_ctx: 8192 in config have no effect.
  • Why it matters: Users cannot control Ollama context window size. A 14B model allocating a 32K context KV cache (~6GB VRAM) causes 8+ minute response times where an 8K context would respond in seconds.
  • What changed: Added resolveUserNumCtx() helper that checks agents.defaults.models.*.params.num_ctx via resolveExtraParams. Both the OpenAI-compatible adapter path (wrapOllamaCompatNumCtx) and native Ollama API path (createOllamaStreamFn) now use user-configured num_ctx when present, falling back to model-discovery contextWindow otherwise.
  • What did NOT change (scope boundary): Model discovery logic, /api/show parsing, extra-params resolution, config schema

Change Type

  • Bug fix

Scope

  • Skills / tool execution

Linked Issue/PR

  • Closes #44550

User-visible / Behavior Changes

When params.num_ctx is set in openclaw.json model config (e.g. "ollama/qwen2.5:14b-8k": { "params": { "num_ctx": 8192 } }), Ollama will now use that value instead of the GGUF metadata context_length. ollama ps will show the user-configured context size.

Security Impact

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No

Repro + Verification

Steps

  1. Create a custom Ollama model with PARAMETER num_ctx 8192 in its modelfile
  2. Set params.num_ctx: 8192 in openclaw.json under the model config block
  3. Restart OpenClaw and send a message
  4. Run ollama ps

Expected

  • Context shows 8192

Actual (before fix)

  • Context shows 32768 (from GGUF metadata)

Evidence

  • Failing test/log before + passing after
  • 68/68 attempt tests passed, 32/32 ollama-stream tests passed, 79/79 extra-params tests passed

Human Verification

  • Verified scenarios: pnpm tsgo passing (only pre-existing errors in twitch/ui); all related tests passing
  • Edge cases checked: undefined config, missing num_ctx param, num_ctx not a number
  • What you did NOT verify: runtime end-to-end testing with actual Ollama service

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.

Compatibility / Migration

  • Backward compatible? Yes — when no user num_ctx is configured, behavior is unchanged
  • Config/env changes? No
  • Migration needed? No

Failure Recovery

  • How to disable/revert: revert this commit

Risks and Mitigations

None

Changed files

  • docs/.generated/config-baseline.json (modified, +1696/-108)
  • src/agents/ollama-stream.test.ts (modified, +32/-0)
  • src/agents/ollama-stream.ts (modified, +9/-3)
  • src/agents/pi-embedded-runner/run/attempt.test.ts (modified, +42/-0)
  • src/agents/pi-embedded-runner/run/attempt.ts (modified, +67/-8)
  • src/context-engine/registry.ts (modified, +67/-9)
RAW_BUFFERClick to expand / collapse

Bug type

Behavior bug (incorrect output/state without crash)

Summary

OpenClaw ignores user-configured num_ctx in openclaw.json when calling Ollama; on every startup it queries /api/show, reads the GGUF metadata field qwen2.context_length: 32768, and passes that value as num_ctx in every /api/chat request, making params.num_ctx: 8192 in openclaw.json have no effect.

Steps to reproduce

  1. Create a custom Ollama model with PARAMETER num_ctx 8192 in its modelfile.
  2. Set params.num_ctx: 8192 in openclaw.json under the model's config block (e.g. "ollama/qwen2.5:14b-8k": { "params": { "num_ctx": 8192 } }).
  3. Restart OpenClaw and send a message via Telegram channel.
  4. Run ollama ps — context shows 32768, not 8192.

Expected behavior

params.num_ctx in openclaw.json should override the discovered contextWindow from /api/show when making Ollama API calls. User config should take priority over model metadata discovery. Running ollama ps should show CONTEXT: 8192.

Actual behavior

  • models.json is regenerated on every startup, overwriting manual edits.
  • ollama ps always shows CONTEXT: 32768.
  • Ollama log confirms n_ctx = 32768.
  • Adding "contextWindow": 8192 to the openclaw.json model block returns: Unrecognized key: "contextWindow".
  • The 14B model allocates a 32K context KV cache (~6GB VRAM), causing 8+ minute response times where an 8K context would respond in seconds.

OpenClaw version

2026.3.8 (3caab92)

Operating system

macOS, Mac mini M4, 24GB unified memory

Install method

No response

Model

ollama/qwen2.5:14b-8k

Provider / routing chain

openclaw -> ollama

Config file / key location

~/.openclaw/openclaw.json ; agents.defaults.models["ollama/qwen2.5:14b-8k"].params.num_ctx

Additional provider/model setup details

Config block in openclaw.json: "ollama/qwen2.5:14b-8k": { "params": { "num_ctx": 8192 } }

Ollama modelfile confirms 8K default: PARAMETER num_ctx 8192

On startup, OpenClaw queries /api/show, reads qwen2.context_length: 32768 from GGUF metadata, writes contextWindow: 32768 into agents/main/agent/models.json, and passes num_ctx: 32768 in all /api/chat requests.

Logs, screenshots, and evidence

Impact and severity

Affected: Ollama users on macOS with large models Severity: High (blocks practical use — 8+ minute response times vs seconds) Frequency: 100% repro Consequence: 14B model allocates 32K KV cache (~6GB VRAM) instead of 8K; system becomes unusable

Additional information

Workaround: Using anthropic/claude-haiku-4-5 as primary model, with Ollama as fallback.

The bug is that models.json is regenerated on every startup, so manual edits to contextWindow are lost. The params.num_ctx value from openclaw.json is never applied to override the discovered GGUF context_length.

extent analysis

Fix Plan

To fix the issue, we need to modify the OpenClaw code to prioritize the params.num_ctx value from openclaw.json over the discovered context_length from the GGUF metadata.

  • Modify the models.json generation code to check for a num_ctx value in the openclaw.json config block and use it if present.
  • Update the /api/chat request code to use the num_ctx value from the config block instead of the discovered context_length.

Example code changes:

# In models.json generation code
if 'params' in model_config and 'num_ctx' in model_config['params']:
    context_window = model_config['params']['num_ctx']
else:
    context_window = gguf_metadata['qwen2.context_length']

# In /api/chat request code
if 'params' in model_config and 'num_ctx' in model_config['params']:
    num_ctx = model_config['params']['num_ctx']
else:
    num_ctx = context_window

Verification

To verify the fix, restart OpenClaw and send a message via Telegram channel. Then, run ollama ps to check if the context shows the correct value (8192). Also, check the Ollama log to confirm that n_ctx is set to 8192.

Extra Tips

  • Make sure to update the openclaw.json file with the correct num_ctx value for the model.
  • If using a large model, consider reducing the num_ctx value to prevent high memory usage.
  • Test the fix with different models and num_ctx values to ensure it works as expected.

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…

FAQ

Expected behavior

params.num_ctx in openclaw.json should override the discovered contextWindow from /api/show when making Ollama API calls. User config should take priority over model metadata discovery. Running ollama ps should show CONTEXT: 8192.

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 - ✅(Solved) Fix [Bug]: agents.defaults.models.*.params.num_ctx ignored for Ollama — model discovery always overrides with GGUF context_length [2 pull requests, 1 participants]