Why Code Execution?
Large language models struggle with:- Complex math and calculations
- Data processing and analysis
- Generating charts and visualizations
- Precise quantitative tasks
How It Works
Task Detection
Khoj identifies when code would be helpful:
- Mathematical calculations
- Data manipulation
- File processing
- Chart generation
Secure Execution
Code runs in an isolated sandbox environment:
- No access to your files or network
- Limited execution time
- Controlled resource usage
Khoj automatically decides when to use code execution. You can also explicitly request it with the
/code command.Using Code Execution
- Automatic
- Explicit /code Command
Simply ask for computational tasks:Khoj detects the need for code and runs it automatically.
Example Use Cases
Mathematics
Mathematics
Complex Calculations:Statistical Analysis:Financial Math:
Data Analysis
Data Analysis
Processing Lists:Trend Analysis:Data Transformation:
Visualizations
Visualizations
Charts:Graphs:Comparisons:
Text Processing
Text Processing
Analysis:Manipulation:Parsing:
Simulations
Simulations
Probability:Monte Carlo:Modeling:
What You’ll See
When Khoj runs code, you get:- Code Block
- Output
- Visualizations
- Errors
The actual Python code that was executed:
Supported Python Libraries
- Always Available
- Sandbox Dependent
Standard Library:
- math, statistics, random
- datetime, time
- json, csv
- re (regex)
- collections, itertools
- numpy
- pandas (basic operations)
- scipy
- matplotlib
- seaborn (if available)
If you need a specific library, ask! Khoj will attempt to use it or suggest alternatives.
Limitations
No File System Access
No File System Access
Code cannot read or write to your actual files.Workaround: Copy-paste data into the chat for processing.
No Network Access
No Network Access
Cannot make HTTP requests or access external APIs.Workaround: Use
/online search first, then process data with /code.Execution Time Limits
Execution Time Limits
Long-running code will timeout (typically 30-60 seconds).Workaround: Optimize algorithms or reduce problem size.
Memory Limits
Memory Limits
Large datasets may exceed available memory.Workaround: Work with smaller samples or summaries.
Simple Code Only
Simple Code Only
Best for straightforward scripts, not complex applications.Limitation: Multi-file projects or complex architectures not supported.
Self-Hosting Setup
Configure code execution for your self-hosted instance:Option 1: Terrarium (Recommended)
Using Docker Compose
Automatic setup with the standard Terrarium sandbox starts automatically!
docker-compose.yml:- Free and open source
- Runs locally
- No API keys needed
- Lightweight
- Limited Python package set
- Basic sandbox features
Option 2: E2B Sandbox
Benefits:
- Wide Python package support
- More powerful sandbox
- pip install capability
- Better for complex code
- Pay per usage
- See E2B pricing
Best Practices
Be Specific
Clearly describe what you want calculated or analyzed
Provide Data
Include actual numbers or datasets in your message
Verify Results
Review the code to understand how the answer was reached
Iterate
Ask follow-up questions to refine or expand analysis
Advanced Examples
Combining with Other Features
- Code + Online Search
- Code + Your Documents
- Code + Automations
Research data, then analyze:
Troubleshooting
Code execution not working
Code execution not working
Self-hosted:
- Verify Terrarium is running:
docker ps | grep terrarium - Check Terrarium logs:
docker logs <container-id> - Test sandbox directly (see verification step above)
- Ensure E2B_API_KEY is valid (if using E2B)
- Restart Khoj server
- Feature should work automatically
- Try explicit
/codecommand - Report if persistent issue
Code errors or failures
Code errors or failures
Common issues:
- Library not available in sandbox
- Syntax error in generated code
- Timeout due to complexity
- Simplify the request
- Ask Khoj to “try again with different approach”
- Request specific library usage
- Check error message and iterate
Missing Python packages
Missing Python packages
Terrarium:
- Limited to pre-installed packages
- Ask Khoj to use alternatives
- Most packages available
- Ask Khoj to pip install if needed
- Use built-in libraries
- Implement functionality manually
Slow execution
Slow execution
Causes:
- Complex calculations
- Large datasets
- Inefficient algorithms
- Reduce problem size
- Ask for optimized approach
- Use sampling for large data
Security & Safety
Code execution is safe:
- Runs in isolated sandbox
- No access to your files or network
- Cannot install system packages
- Execution time limited
- Memory constrained
- Separate from Khoj server
Next Steps
Online Search + Code
Combine web research with data analysis
Automate Analysis
Schedule recurring code execution tasks
Chat Commands
Learn more slash commands and features
