This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
# Activate virtual environment
source .venv/bin/activate
# Install/sync dependencies (using uv)
uv sync --locked# Start the API server (uses HOST/PORT env vars, defaults to 0.0.0.0:8000)
code-interpreter-api# Type checking - MUST pass strict mypy
mypy .
# Linting and formatting
ruff check .
ruff format .
# Run all pre-commit hooks
pre-commit run --all-files# Run integration tests
pytest tests/integration_tests -q
# Run with environment variables from VS Code
source .venv/bin/activate && python -m dotenv -f .vscode/.env run -- pytest tests/integration_tests -q
# Run a specific test
pytest tests/integration_tests/test_file.py::test_function_name -vThis is a FastAPI-based code execution API that safely runs Python code in sandboxed environments. The system uses Docker containers for secure isolation.
-
Layered Architecture:
- API routes in
app/api/handle HTTP requests - Business logic in
app/services/manages execution backends - Data models in
app/models/define request/response schemas - Configuration in
app/core/handles environment-based settings
- API routes in
-
Execution Backend Abstraction:
- The service supports swappable execution strategies via a plugin pattern
executor_docker.py: Uses Docker containers for stronger isolation- Selection is environment-based, allowing runtime configuration
-
Security-First Design:
- All code execution happens in isolated environments with strict resource limits
- Configurable timeouts (MAX_EXEC_TIMEOUT_MS), memory limits (MEMORY_LIMIT_MB), and output size limits (MAX_OUTPUT_BYTES)
- No direct filesystem access; files are staged through base64 encoding
-
Type Safety Enforcement:
- Strict mypy checking is mandatory - all code must pass
mypy --strict - All API contracts use Pydantic models for automatic validation
- Comprehensive type annotations throughout the codebase
- Strict mypy checking is mandatory - all code must pass
- Main API Endpoint:
POST /v1/executeaccepts Python code and optional file inputs - Configuration: Uses frozen dataclasses with LRU caching for immutable, efficient settings
- Error Handling: Structured exceptions with proper HTTP status codes and validation error details
- File Handling: Files are passed as base64-encoded content with specified paths for workspace staging
- Testing: Integration tests in
tests/integration_tests/cover various execution scenarios
- Always ensure mypy strict mode passes before committing
- Add integration tests for new execution scenarios
- Follow existing module structure: API routes → services → models
- Use environment variables for configuration, never hardcode values
- Maintain the security boundaries - never bypass sandbox restrictions
There are two main kinds of tests:
- Under the
code-interpreter/tests/integration_testsdirectory - Doesn't require anything to be running - spins up a dummy FastAPI server for each run
- Primary way of testing functionality
- Under the
code-interpreter/tests/e2edirectory - Requires the code-interpreter service to be running. Usually as a Docker container.
- After making changes, if you want to run these tests make sure to (1) stop existing containers,
(2) build new images, and (3) run the new containers.