Overview
Before you can start using Swarms, you need to configure your environment with the necessary API keys and settings. This guide will walk you through setting up your environment variables and workspace directory.Swarms supports multiple LLM providers including OpenAI, Anthropic, Groq, and more. You only need to configure the API keys for the providers you plan to use.
Quick Setup
The fastest way to get started is to create a.env file in your project root:
Required Environment Variables
Core Configuration
The directory where agents will store their outputs, logs, and temporary files.
LLM Provider API Keys
Configure the API keys for the LLM providers you want to use:Your Anthropic API key for using Claude models (claude-sonnet-4-5, etc.)Get your key: Anthropic Console
Complete .env File Example
Here’s a complete example of a.env file with all supported providers:
.env
Workspace Directory Setup
The workspace directory is where agents store their outputs, logs, and state:Default Structure
Custom Workspace Configuration
You can customize the workspace location:Loading Environment Variables
There are several ways to load your environment variables:Method 1: Using python-dotenv (Recommended)
Method 2: Manual Loading
Method 3: System Environment Variables
- Linux/macOS
- Windows (PowerShell)
- Windows (CMD)
Getting API Keys
Here’s where to get API keys for each supported provider:OpenAI
Create an account and generate API keys from the OpenAI platform
Anthropic
Sign up for Anthropic Claude and get your API key from the console
Groq
Register for Groq’s fast inference and generate your API key
Cohere
Create a Cohere account and access your API keys
DeepSeek
Sign up for DeepSeek and generate your API credentials
OpenRouter
Access multiple models through OpenRouter with a single API key
Verifying Your Setup
Run this script to verify your environment is configured correctly:verify_setup.py
Environment Best Practices
Use different environments for dev/prod
Use different environments for dev/prod
Create separate Load the appropriate one:
.env files for different environments:Validate required variables
Validate required variables
Always validate that required environment variables are set:
Use secret management in production
Use secret management in production
For production deployments, use proper secret management:
- AWS: AWS Secrets Manager or Parameter Store
- Azure: Azure Key Vault
- GCP: Google Secret Manager
- Kubernetes: Kubernetes Secrets
- Docker: Docker Secrets
Rotate API keys regularly
Rotate API keys regularly
- Set up a key rotation schedule (e.g., every 90 days)
- Monitor API key usage for anomalies
- Use separate keys for different applications
- Revoke unused or compromised keys immediately
Troubleshooting
Environment variables not loading
Environment variables not loading
- Verify the
.envfile is in the correct directory - Check that you’re calling
load_dotenv()before accessing variables - Ensure there are no syntax errors in the
.envfile - Try printing
os.getcwd()to verify current directory
API key authentication errors
API key authentication errors
- Verify the API key is correct (no extra spaces or quotes)
- Check that the key has the necessary permissions
- Ensure the key hasn’t expired or been revoked
- Test the key directly with the provider’s API
Workspace directory errors
Workspace directory errors
- Ensure the directory path exists or can be created
- Check file system permissions
- Use absolute paths to avoid resolution issues
- Verify sufficient disk space
Next Steps
Quickstart Guide
Create your first agent now that your environment is configured
Model Providers
Learn about all supported LLM providers and how to use them
For more detailed information about environment configuration, visit the official documentation.