Why this matters
Humans and agents work best with ephemeral environments - one environment per task, treated like cattle not pets. This isolation means:- No state conflicts: Each task gets a clean slate
- Parallel execution: Multiple agents work simultaneously without interference
- Mistakes are free: If something goes wrong, discard the environment and start fresh - no damage to your local machine or endpoint
Optimization strategies
Use prebuilds
Prebuilds run your environment setup ahead of time - container builds, dependency installation, and startup tasks. When an agent starts an environment, it loads from a prebuild snapshot instead of running setup from scratch. This is the single biggest optimization for agent workflows. Configure prebuilds to run on pushes to main and your environments will be ready when agents need them.Enable Dev Container image caching
For runners in your VPC, enable image caching to avoid rebuilding containers:- AWS runners: Caches built Dev Container images automatically
- GCP runners: Supports image caching through Artifact Registry
Optimize your Dockerfile
Structure your Dockerfile for caching efficiency:- Put stable dependencies first: System packages, build tools, and runtimes that rarely change should be early in the Dockerfile
- Put volatile dependencies last: Application dependencies that change frequently go at the end
- Use prebuilt base images: Push your base image to a registry and reference it in devcontainer.json
Use tasks for dynamic setup
For setup that must happen at runtime (database seeding, environment-specific config), use Tasks rather than baking everything into the image.One environment per task
Rather than reusing environments across tasks, create a fresh environment for each piece of work. This matches how agents operate most effectively:- Agent receives a task
- Agent starts a clean environment
- Agent completes the work and creates a PR
- Environment is discarded
Measuring startup time
Track your environment startup times to identify bottlenecks:- Container build time: How long to build the Dev Container image?
- Dependency installation: How long do tasks take to run?
- Prebuild freshness: Are prebuilds running often enough?