Skip to main content

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:
1

Create a .env file

Create a file named .env in your project’s root directory:
2

Add your API keys

Add your API keys to the .env file:
3

Verify the setup

Load and verify your environment variables:

Required Environment Variables

Core Configuration

string
default:"agent_workspace"
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:
string
required
Your OpenAI API key for using GPT models (gpt-4, gpt-4o-mini, etc.)Get your key: OpenAI API Keys
string
Your Anthropic API key for using Claude models (claude-sonnet-4-5, etc.)Get your key: Anthropic Console
string
Your Groq API key for using Groq’s fast LLM inferenceGet your key: Groq Console

Complete .env File Example

Here’s a complete example of a .env file with all supported providers:
.env
Security Best Practices:
  • Never commit your .env file to version control
  • Add .env to your .gitignore file
  • Use different API keys for development and production
  • Rotate your API keys regularly

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:
Use absolute paths for workspace directories in production environments to avoid path resolution issues.

Loading Environment Variables

There are several ways to load your environment variables:

Method 2: Manual Loading

Method 3: System Environment Variables

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
Run the verification script:

Environment Best Practices

Create separate .env files for different environments:
Load the appropriate one:
Always validate that required environment variables are set:
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
  • 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

  1. Verify the .env file is in the correct directory
  2. Check that you’re calling load_dotenv() before accessing variables
  3. Ensure there are no syntax errors in the .env file
  4. Try printing os.getcwd() to verify current directory
  1. Verify the API key is correct (no extra spaces or quotes)
  2. Check that the key has the necessary permissions
  3. Ensure the key hasn’t expired or been revoked
  4. Test the key directly with the provider’s API
  1. Ensure the directory path exists or can be created
  2. Check file system permissions
  3. Use absolute paths to avoid resolution issues
  4. 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.