Install
openclaw skills install claude-api-cost-optimizerMinimize Anthropic Claude API costs through model selection, prompt caching, batching, and cost tracking. ⚠️ Enables local caching and cost logging — review file permissions and exclude secrets. Trigger phrases: reduce API costs, optimize Claude spending, save on API calls, Claude cost optimization, cheaper Claude models.
openclaw skills install claude-api-cost-optimizer⚠️ Local Data Warning: This skill creates
~/.claude_cache/,costs.log, andbatch_results.jsonlwhich may contain source code, API responses, and prompts. Setchmod 600on these files, never cache secrets/PII, and add cache directories to.gitignore.
Cut Claude API costs by 70–90% using intelligent model selection, caching, and batching.
ANTHROPIC_API_KEY is set in your environment (required).references/pricing.md for current rates."cache_control": {"type": "ephemeral"} to message blocksinput_tokens, output_tokens, and cache metrics from API responsesreferences/pricing.md for current rates.Code examples are in Python but concepts apply to any language or SDK.
Model selection pattern:
def select_model(task_type: str) -> str:
simple_tasks = ["formatting", "comments", "explanation", "rename"]
complex_tasks = ["architecture", "algorithm", "security_audit"]
return ("claude-haiku-4-5-20251001" if task_type in simple_tasks else
"claude-opus-4-6" if task_type in complex_tasks else
"claude-sonnet-4-6")
Prompt caching:
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
system=[{
"type": "text",
"text": system_prompt,
"cache_control": {"type": "ephemeral"}
}],
messages=[{
"role": "user",
"content": [
{"type": "text", "text": f"Code:\n{source_code}",
"cache_control": {"type": "ephemeral"}},
{"type": "text", "text": query}
]
}]
)
Cost tracking:
usage = response.usage
cost = (usage.input_tokens * INPUT_RATE +
usage.cache_creation_input_tokens * CACHE_WRITE_RATE +
usage.cache_read_input_tokens * CACHE_READ_RATE +
usage.output_tokens * OUTPUT_RATE)
Before using cost tracking and local caching:
chmod 600 ~/.claude_cache/ and all log filesmax_age_hours) and clear cache regularly.gitignorereferences/implementation.md — Full implementation patterns, model routing, caching setup, batching, retry logic, and anti-patternsreferences/pricing.md — Current pricing, cache cost math, savings calculations, and batch API details