intermediate45 minLesson 4 of 6

Performance Optimization and Caching

Optimize OpenCode for speed and cost efficiency. Learn caching strategies, token optimization, model selection, and how to reduce API calls while maintaining quality.

Performance Optimization and Caching

Why Optimize?

MetricImpact
Response TimeFaster development workflow
Token UsageLower API costs
QualityBetter results with less overhead
ScalabilityHandle larger projects

Token Optimization

Understanding Tokens

Tokens are the basic units LLMs process:

ContentApproximate Tokens
1 word1-2 tokens
1 line of code5-15 tokens
1 file (100 lines)500-1500 tokens

Reduce Token Usage

  1. Clear conversation regularly:
> /clear
  1. Be specific in prompts:
❌ "Fix the bug" ✅ "Fix the ZeroDivisionError in calculate_average() when list is empty"
  1. Read only relevant files:
❌ "Read all files in src/" ✅ "Read src/utils.py"
  1. Use grep before reading:
> Search for "TODO" in src/ before reading entire files

Model Selection Strategy

TaskModelReason
Simple questionsgpt-4o-miniFast, cheap
Code generationgpt-4oBalanced
Complex reasoningclaude-sonnet-4-20250514High quality
Code reviewclaude-sonnet-4-20250514Thorough analysis
Quick editsgpt-4o-miniSpeed

Configuration

json
{ "agents": { "quick": { "model": "gpt-4o-mini", "description": "Simple tasks, fast responses" }, "complex": { "model": "gpt-4o", "description": "Complex tasks, high quality" } } }

Caching Strategies

File Content Cache

Cache frequently accessed files:

python
# Pseudo-code for caching file_cache = {} def read_file(path): if path in file_cache: return file_cache[path] content = read_from_disk(path) file_cache[path] = content return content

Prompt Cache

Reuse prompt templates:

json
{ "skills": { "code-review": { "prompt_template": "Review this code for security issues: {code}" } } }

Result Cache

Cache AI responses for repeated queries:

json
{ "cache": { "enabled": true, "ttl": 3600, "maxSize": 1000 } }

Batch Processing

Process Multiple Files

Instead of:

> Read file1.py > Read file2.py > Read file3.py

Use:

> Read all Python files in src/ and summarize their purposes

Parallel Requests

json
{ "performance": { "parallelRequests": true, "maxConcurrent": 3 } }

Context Window Management

Monitor Context Size

> /verbose Context: 4523 tokens (15% of 32k limit)

Optimize Context

  1. Clear history when switching topics
  2. Read only necessary files
  3. Summarize long conversations
  4. Use subagents for isolated tasks

Network Optimization

Connection Pooling

json
{ "providers": { "openai": { "keepAlive": true, "maxConnections": 5 } } }

Request Batching

json
{ "performance": { "batchRequests": true, "batchSize": 10 } }

Performance Monitoring

Track Metrics

json
{ "logging": { "performance": true, "tokenUsage": true, "responseTime": true } }

Analyze Logs

bash
# Find slow requests grep "response_time" opencode.log | awk '{print $NF}' | sort -n # Calculate average tokens grep "total_tokens" opencode.log | awk '{sum+=$NF} END {print sum/NR}'

Practice Questions

Practice Question

What is the most effective way to reduce token usage?

Practice Question

Which model is best for quick, simple tasks?

Practice Question

How does caching improve performance?

Practice Question

What should you monitor to track performance?

Practice Question

How do you reduce context window usage?


Success

Key Takeaways

  • Clear conversation regularly to reduce context size
  • Be specific in prompts to minimize token usage
  • Use gpt-4o-mini for simple tasks, gpt-4o for complex ones
  • Cache frequently accessed files and prompt templates
  • Monitor token usage, response time, and cost
  • Batch process multiple files instead of reading one at a time
  • Network optimization reduces latency for API calls
Progress67%