A practical guide to cutting operational costs and improving quality by mastering openclaw.json
The Hidden Cost Problem Nobody Talks About
OpenClaw is a powerful open-source AI agent that runs 24/7 on your device. But out of the box, its default configuration is optimized for ideal conditions
Here’s what’s silently draining your token budget:
- Every new message causes the system to re-send the entire conversation history
- Background checks (heartbeats) fire automatically every 30 minutes
- Workspace files get reloaded on every single chat turn
Most of what you’re paying for is overhead, not actual work getting done.
The good news: all of this is controllable through a single file – openclaw.json.
Tweaking its defaults can cut your costs by 60-80%.
Understanding the Real Numbers
Before optimizing, you need to understand what you’re actually paying for.
Cost = Number of tokens × Model price per token
Here’s what monthly costs look like at default settings with different models:
| Model | Estimated Monthly Cost |
|---|---|
| Claude Opus 4.6 | ~$325 / month |
| Claude Sonnet 4.6 | ~$188 / month |
| Gemini 2.5 Flash | ~$24 / month |
| GPT-OSS-120B | ~$2 / month |
Most of that money isn’t paying for useful work, it’s paying for redundant context being passed around on every loop.
The Optimization Playbook
1. Optimize the Heartbeat First
The heartbeat is the agent’s “pulse” – it wakes up periodically to run background tasks even when no one is talking to it. By default, this runs every 30 minutes with a full context load.
The cost formula for heartbeat: Cost = Number of heartbeats × (tokens per beat × model price)
Here’s the optimized configuration:
{
"heartbeat": {
"every": "55m",
"lightContext": true,
"isolatedSession": true,
"skipWhenBusy": true,
"activeHours": { "start": "08:00", "end": "22:00" },
"model": "ollama/llama3.2:1b"
}
}
The single most impactful line here is “isolatedSession”: true.
This alone reduces heartbeat token usage from ~100K tokens down to ~2–5K tokens per beat
because the agent runs in a clean context instead of carrying the full conversation history with it.
Additional savings come from:
- Running only during active hours (not burning tokens at 3am)
- Using a lightweight local model (free!) for background tasks
- Skipping the heartbeat when the agent is already busy
2. Limit Context Injection
By default, every time the agent picks up a new task, it re-injects all previous context into memory. In a 20-turn session, turn 18 is loading the same context as turn 1.
Fix it:
{
"contextInjection": "continuation-skip",
"bootstrapMaxChars": 12000,
"bootstrapTotalMaxChars": 60000
}
Setting contextInjection to “continuation-skip” means the agent won’t reload identical context it already has. In a 20-turn session, turn 18 skips the redundant context load entirely.
This is configured under agents.defaults.contextInjection.
3. Prune Tool Results
When a tool finishes running, its inputs and outputs don’t need to live in the context window forever.
{
"contextPruning": {
"mode": "cache-ttl",
"ttl": "1h"
}
}
This automatically discards tool call history older than 1 hour, eliminating the “dead weight” tokens that accumulate from completed operations.
Configured under agents.defaults.contextPruning.
4. Upgrade Your Memory
Poor memory is expensive. When an agent forgets what it learned in a previous session, it has to re-discover, re-test, and re-verify – burning tokens all over again.
Without good memory:
- Agent forgets what it was taught at the start of each session
- Agent re-explores, retries, and fails more – increasing token count and request volume
With good memory:
- Agent recalls previous learnings before starting each task
- Fewer wrong turns, faster execution, lower cost
Save memory before context fills up:
{
"compaction": {
"memoryFlush": {
"enabled": true,
"model": "ollama/qwen3:8b",
"prompt": "Write lasting notes to memory/YYYY-MM-DD.md. Reply NO_REPLY if nothing to save."
}
}
}
When the context window is nearly full, this triggers the agent to write important insights to a dated memory file before summarizing and compressing the context. Use a free local model here to keep costs at zero.
Enable natural language memory search:
{
"memorySearch": {
"provider": "openai"
}
}
This activates hybrid search – combining keyword matching with semantic vector similarity. Instead of Ctrl+F – style keyword hunting, the agent can retrieve the right memory with a natural language query, leading to more accurate and faster task execution.
Configured under agents.defaults.memorySearch.
5. Always Have a Fallback Model
Don’t let a rate limit or outage grind your agent to a halt.
{
"model": {
"primary": "anthropic/claude-opus-4-6",
"fallbacks": [
"anthropic/claude-sonnet-4-6",
"google/gemini-2.5-flash"
]
}
}
The agent automatically switches to the next model when it encounters rate limits, overload errors, or connection failures — with zero interruption to ongoing tasks.
Configured under agents.defaults.model.fallbacks.
6. Cache the System Prompt
Your agent’s system prompt is re-processed on every single request by default. Enabling cache retention means the model temporarily memorizes it instead.
{
"params": {
"cacheRetention": "long"
}
}
Benefits:
- Faster token generation — no need to re-vectorize the system prompt
- Up to 90% cost reduction on cached tokens when using Anthropic models
Three retention levels are available: none, short, and long — choose based on how long you want the cached computation to persist.
Configured under agents.defaults.params.cacheRetention.
7. Optimize Scheduled Tasks (Heartbeat Tasks)
When the agent is idle and no one is chatting with it – what does it do?
By default, it runs tasks listed in HEARTBEAT.md every 30 minutes: housekeeping, email checks, directory scans. This gives the agent its “alive” quality.
The key optimization: only run tasks when they’re actually due.
# HEARTBEAT.md
tasks:
- id: inbox-check
interval: 1h
prompt: "Check inbox. Reply HEARTBEAT_OK if nothing urgent."
- id: memory-consolidate
interval: 12h
prompt: "Summarize memory files into MEMORY.md."
- id: weekly-review
interval: 7d
prompt: "Write weekly review to memory/weekly-YYYY-MM-DD.md."
When no task is due, token cost is exactly $0. The agent checks its task list, finds nothing scheduled, and goes back to sleep.
The Complete Optimized openclaw.json
Here’s everything combined into a single production-ready configuration:
{
"agents": {
"defaults": {
"contextInjection": "continuation-skip",
"bootstrapMaxChars": 12000,
"bootstrapTotalMaxChars": 60000,
"contextPruning": { "mode": "cache-ttl", "ttl": "1h" },
"model": {
"primary": "anthropic/claude-sonnet-4-6",
"fallbacks": ["openai/gpt-oss-120b", "google/gemini-2.5-flash"]
},
"params": { "cacheRetention": "long" },
"heartbeat": {
"every": "55m",
"lightContext": true,
"isolatedSession": true,
"skipWhenBusy": true,
"activeHours": { "start": "08:00", "end": "22:00" },
"model": "ollama/llama3.2:1b"
},
"compaction": {
"memoryFlush": {
"enabled": true,
"model": "ollama/qwen3:8b",
"prompt": "Write lasting notes to memory/YYYY-MM-DD.md. Reply NO_REPLY if nothing to save."
}
},
"memorySearch": { "provider": "openai" }
}
}
}
You can even ask your agent to apply this directly: “Update my openclaw.json with this configuration.”
The Continuous Optimization Loop
The most important principle: don’t optimize before you feel the pain. Premature optimization wastes time. Instead, follow this cycle:
Observe → Evaluate → Improve → Measure → Repeat
Priority order for optimization:
- Heartbeat — biggest single lever for cost reduction
- Context — eliminates redundant token loads
- Memory — reduces retry loops and re-exploration
- LLM Model — right-size the model to the task
How to execute each phase:
- Observe: Check model costs and task quality before touching anything
- Evaluate: Use logs to find which tasks consume the most tokens
- Improve: Change one configuration at a time, re-observe
- Measure: Quantify the cost reduction and quality change
Summary
OpenClaw is already a capable autonomous agent. With proper configuration, it becomes a cost-efficient one. The key levers — heartbeat isolation, context skipping, tool result pruning, memory persistence, model fallbacks, and prompt caching — work together to dramatically reduce waste without sacrificing capability.
The full optimization stack can realistically bring costs from $188/month down to under $30/month on the same workload, while actually improving response quality through better memory and smarter scheduling.