openclaw - 💡(How to fix) Fix [Bug]: Tool results duplicated in session context — 3x token overhead with OmniRoute gateway [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#53803Fetched 2026-04-08 01:23:17
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
1
Author
Participants
Timeline (top)
labeled ×2
Session context fills 3-4x faster than expected due to tool result duplication (`content[].text` + `details.aggregated`) and verbose OmniRoute metadata added to every message.

Root Cause

Root cause hypothesis:

  1. details.aggregated field duplicates content[].text — should be removed or excluded from token counting
  2. OmniRoute metadata (provider, api, model, responseId, stopReason, usage) added to every message — should be optional or excluded from context

Fix Action

Fix / Workaround

Additional information

**Workaround applied:**
Reduced compaction thresholds:
```json
{
  "compaction": {
    "reserveTokens": 12000,
    "keepRecentTokens": 15000,
    "maxHistoryShare": 0.5
  }
}

Code Example

Tool results duplicated in session context — 3x token overhead with OmniRoute gateway

---

Regression (worked before, now fails)

---

Session context fills 3-4x faster than expected due to tool result duplication (`content[].text` + `details.aggregated`) and verbose OmniRoute metadata added to every message.

---

1. Configure OpenClaw with OmniRoute gateway (local or remote)
2. Run a session with multiple tool calls (exec, web_fetch, searxng_search, sessions_list)
3. Check session file size: `wc -l ~/.openclaw/agents/main/sessions/*.jsonl`
4. Monitor context usage: `/status` shows rapid growth (~50k tokens per turn)

**Observed in my setup:**
- Session file: 152 lines, 536K on disk
- Context usage: 127k/200k tokens (63%) after just a few turns
- One cron tool call showed 88,275 input tokens

---

Tool results should be stored once without duplication. Context growth should be comparable to direct model connections (e.g., openai-codex saves ~15-20k tokens per turn with similar workload).

---

Each tool result is saved with BOTH:
- `content[].text` — full tool output
- `details.aggregated` — same output duplicated

Plus OmniRoute adds to EVERY message:
- `provider`, `api`, `model`, `responseId`, `stopReason`, `usage`

**Example from session log:**

---

**Impact on my session:**
- 127k tokens consumed in ~5-7 turns
- ~40-50k tokens = overhead from duplication + metadata
- Context exhausted at 63% capacity prematurely

---

2026.3.23-2 (7ffe7e4)

---

Ubuntu 24.04.4 LTS (Noble Numbat)Linux 6.8.0-100-generic (x64)

---

npm global

---

omniroute/main-copy

---

openclaw -> OmniRoute (localhost:20128) -> multiple providers (OpenRouter, iFlow, Kiro, NVIDIA, Gemini, etc.)

---

OmniRoute configuration:
- Local gateway at `http://127.0.0.1:20128/v1`
- 44+ providers configured with fallback chains
- Logging level: info (includes usage metadata in every response)

OpenClaw compaction settings (before fix):

---



---

**Context usage from /status:**
- Tokens: 356k in / 1.1k out
- Context: 127k/200k (63%)
- Compactions: 1

**Tool result example (truncated in logs):**

---

**Affected:** All OpenClaw users with OmniRoute or similar gateway setups

**Severity:** High — reduces effective context window by 3-4x, forces premature compaction

**Frequency:** Always — occurs on every tool call with gateway-based models

**Consequence:**
- Conversation history lost earlier due to aggressive compaction needs
- Increased token costs if provider charges per input token
- Degraded agent performance due to reduced context availability

---

**Workaround applied:**
Reduced compaction thresholds:
RAW_BUFFERClick to expand / collapse

Title

Tool results duplicated in session context — 3x token overhead with OmniRoute gateway

Bug type

Regression (worked before, now fails)

Summary

Session context fills 3-4x faster than expected due to tool result duplication (`content[].text` + `details.aggregated`) and verbose OmniRoute metadata added to every message.

Steps to reproduce

1. Configure OpenClaw with OmniRoute gateway (local or remote)
2. Run a session with multiple tool calls (exec, web_fetch, searxng_search, sessions_list)
3. Check session file size: `wc -l ~/.openclaw/agents/main/sessions/*.jsonl`
4. Monitor context usage: `/status` shows rapid growth (~50k tokens per turn)

**Observed in my setup:**
- Session file: 152 lines, 536K on disk
- Context usage: 127k/200k tokens (63%) after just a few turns
- One cron tool call showed 88,275 input tokens

Expected behavior

Tool results should be stored once without duplication. Context growth should be comparable to direct model connections (e.g., openai-codex saves ~15-20k tokens per turn with similar workload).

Actual behavior

Each tool result is saved with BOTH:
- `content[].text` — full tool output
- `details.aggregated` — same output duplicated

Plus OmniRoute adds to EVERY message:
- `provider`, `api`, `model`, `responseId`, `stopReason`, `usage`

**Example from session log:**
```json
{
  "toolName": "exec",
  "content": [{"type": "text", "text": "...full output..."}],
  "details": {
    "aggregated": "...same output again...",
    "cwd": "/path/..."
  },
  "usage": {"input": 90947, "output": 39, "totalTokens": 90986}
}

Impact on my session:

  • 127k tokens consumed in ~5-7 turns
  • ~40-50k tokens = overhead from duplication + metadata
  • Context exhausted at 63% capacity prematurely

---

## OpenClaw version

2026.3.23-2 (7ffe7e4)


---

## Operating system

Ubuntu 24.04.4 LTS (Noble Numbat) — Linux 6.8.0-100-generic (x64)


---

## Install method

npm global


---

## Model

omniroute/main-copy


---

## Provider / routing chain

openclaw -> OmniRoute (localhost:20128) -> multiple providers (OpenRouter, iFlow, Kiro, NVIDIA, Gemini, etc.)


---

## Additional provider/model setup details
```markdown
OmniRoute configuration:
- Local gateway at `http://127.0.0.1:20128/v1`
- 44+ providers configured with fallback chains
- Logging level: info (includes usage metadata in every response)

OpenClaw compaction settings (before fix):
```json
{
  "compaction": {
    "mode": "safeguard",
    "reserveTokens": 16384,
    "keepRecentTokens": 20000
  }
}

---

## Logs, screenshots, and evidence
```markdown
**Context usage from /status:**
- Tokens: 356k in / 1.1k out
- Context: 127k/200k (63%)
- Compactions: 1

**Tool result example (truncated in logs):**

[... 153441 more characters truncated]


Impact and severity

**Affected:** All OpenClaw users with OmniRoute or similar gateway setups

**Severity:** High — reduces effective context window by 3-4x, forces premature compaction

**Frequency:** Always — occurs on every tool call with gateway-based models

**Consequence:**
- Conversation history lost earlier due to aggressive compaction needs
- Increased token costs if provider charges per input token
- Degraded agent performance due to reduced context availability

Additional information

**Workaround applied:**
Reduced compaction thresholds:
```json
{
  "compaction": {
    "reserveTokens": 12000,
    "keepRecentTokens": 15000,
    "maxHistoryShare": 0.5
  }
}

This reduces growth from ~50k/turn to ~15-20k/turn, but is a band-aid fix.

Comparison with openai-codex: Direct Codex CLI connection shows gradual, efficient context management without this overhead.

Root cause hypothesis:

  1. details.aggregated field duplicates content[].text — should be removed or excluded from token counting
  2. OmniRoute metadata (provider, api, model, responseId, stopReason, usage) added to every message — should be optional or excluded from context

Last known good: openai-codex direct connection (no gateway overhead) First known bad: OmniRoute gateway integration (version unclear)

extent analysis

Fix Plan

To address the issue of tool result duplication and verbose OmniRoute metadata, we will implement the following steps:

  • Modify the OpenClaw configuration to exclude duplicated tool results
  • Configure OmniRoute to reduce metadata in responses

Step-by-Step Solution

  1. Update OpenClaw configuration: Add a filter to exclude details.aggregated from tool results.
{
  "toolResults": {
    "excludeFields": ["details.aggregated"]
  }
}
  1. Configure OmniRoute: Update the OmniRoute configuration to exclude verbose metadata.
{
  "omniroute": {
    "metadata": {
      "excludeFields": ["provider", "api", "model", "responseId", "stopReason", "usage"]
    }
  }
}
  1. Adjust compaction settings: Update the compaction settings to reserve more tokens and keep recent tokens.
{
  "compaction": {
    "reserveTokens": 20000,
    "keepRecentTokens": 30000,
    "maxHistoryShare": 0.6
  }
}

Verification

To verify that the fix worked:

  1. Run a session with multiple tool calls.
  2. Check the session file size using wc -l ~/.openclaw/agents/main/sessions/*.jsonl.
  3. Monitor context usage using /status.
  4. Verify that the context growth is comparable to direct model connections.

Extra Tips

  • Regularly review and update the OpenClaw and OmniRoute configurations to ensure optimal performance.
  • Consider implementing a more efficient compaction strategy to reduce token costs and improve agent performance.

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

Tool results should be stored once without duplication. Context growth should be comparable to direct model connections (e.g., openai-codex saves ~15-20k tokens per turn with similar workload).

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 [Bug]: Tool results duplicated in session context — 3x token overhead with OmniRoute gateway [1 participants]