If you've been running OpenClaw, you might have noticed your API bills skyrocketing. It is incredibly easy to burn through hundreds of dollars when OpenClaw defaults to heavy models and massive context windows. Fortunately, with a few strategic tweaks, you can drop your daily API spend from over $100 to less than $5.
Here is a full breakdown of how to optimize your OpenClaw setup to save massive amounts of money without sacrificing performance.
Step 1: Secure Server Setup and Easy File Editing
Before optimizing, ensure your OpenClaw instance is running securely in an isolated environment, preferably a Virtual Private Server (VPS) like Hostinger. Once your VPS is deployed using Docker, you'll need to edit configuration files.
Instead of using clunky terminal text editors, download Visual Studio Code and install the "Remote - SSH" extension. This allows you to securely connect to your server and edit files (like openclaw.json and soul.md) directly through a clean visual interface.
Step 2: Implement Smart Model Routing
One of the biggest mistakes users make is forcing OpenClaw to use top-tier models like Anthropic's Opus 4.6 for every single operation. 90% of OpenClaw's tasks can be handled by cheaper, faster models.
To fix this, edit your openclaw.json to include API keys from at least two different providers (e.g., Anthropic and OpenAI). This prevents bottlenecks; if Claude hits a rate limit, the system can automatically switch to OpenAI's GPT models.
- Set a cheap default: Change your default model to something inexpensive, like Claude Haiku 4.5.
- Establish routing rules: In your
soul.mdfile, write explicit prompt rules telling the AI to only switch to heavier models (like Sonnet 4.6 or Opus 4.6) for complex reasoning tasks, security reviews, or major decisions. Include fallbacks like GPT-5 Mini and GPT-5.1 if primary providers are unavailable.
Step 3: Optimize Session Initialization
By default, OpenClaw loads days worth of chat history and unnecessary tool outputs every time you start a new session. Because you pay for the size of the request (token count), this is a massive waste of money.
Instruct your agent in soul.md to only load absolute necessities at the start of a session—such as soul.md, user.md, and today's memory log. Tell the system to use a search function to find past context only when specifically asked, rather than blindly loading the whole file.
Step 4: Offload "Heartbeats" to a Free Local Model
A "heartbeat" is a scheduled check-in that OpenClaw runs every 30 to 60 minutes to monitor background tasks. Running these simple check-ins through paid APIs drains your budget.
Instead, install Ollama on your server to run a free, local AI.
- Install Ollama via the terminal.
- Pull a lightweight model that can run easily on your server's CPU, such as Llama 3.2 (3 billion parameters).
- Update the
defaultssection of youropenclaw.jsonto route heartbeat tasks entirely to this local Llama model. This makes all routine background checks 100% free.
Step 5: Enable Prompt Caching and Context Pruning
Prompt caching will be your biggest instant money saver. By caching system prompts, you can save roughly 90% on the cost of reading those repetitive files for every message.
- In
openclaw.json, add a caching parameter to your heavier models. Setcache retentionto "long" (holds for 1 hour) for large models like Opus, and "short" (holds for 5 minutes) for medium models. - Next, add a "context pruning" key to the
defaultssection in your JSON file. This automatically deletes stale and irrelevant context from the API request as a session grows longer, preventing token bloat.
Step 6: Set Hard Spending Limits
Protect yourself from accidental overcharges by setting strict API call pacing and daily/monthly budget rules. Inside your soul.md file, you can explicitly command the bot to pause for 5 seconds between consecutive API calls, and to notify you immediately if daily spend hits a specific target like $3.75 or $5.00.
Step 7: Run Regular Token Audits
Finally, you need to verify your optimizations are working. OpenClaw has built-in commands to help you track your cash hit rates and token usage.
- Type
/statusin the gateway to see your current model, context window size, and live token throughput. - Type
/context listto see exactly which files are injecting tokens into your prompt, allowing you to identify bloated files. - Type
/context detailfor a highly granular breakdown of token distribution.
By implementing these optimizations—caching, model routing, local heartbeats, and strict context limits—you'll create an incredibly efficient OpenClaw agent that runs at a fraction of the cost.