Skip to main content

A multi-agent ecosystem for large language models (LLMs) and autonomous systems.

Project description

๐Ÿ LangSwarm

Multi-Agent AI Orchestration Framework

Build intelligent systems where multiple AI agents collaborate to solve complex tasks. LangSwarm makes it easy to create, orchestrate, and scale AI agent workflows with support for all major LLM providers and a rich ecosystem of tools.

License: MIT Python 3.8+


๐ŸŽฏ What is LangSwarm?

LangSwarm is a framework for multi-agent AI orchestration. Unlike simple chatbot libraries, LangSwarm enables you to:

  • Orchestrate multiple specialized agents working together on complex tasks
  • Build workflows where agents collaborate, hand off work, and combine their outputs
  • Integrate tools through the Model Context Protocol (MCP) for real-world capabilities
  • Support any LLM provider (OpenAI, Anthropic, Google, Mistral, local models, and more)
  • Scale from prototypes to production with enterprise-grade memory, observability, and deployment options

Why Multi-Agent?

Single AI agents hit limits quickly. Multi-agent systems unlock:

  • Specialization: Each agent excels at specific tasks (research, writing, analysis, coding)
  • Collaboration: Agents work together, combining strengths and compensating for weaknesses
  • Scalability: Distribute workload across multiple agents and providers
  • Reliability: Redundancy and validation through multiple perspectives
  • Modularity: Build, test, and deploy agents independently

โšก Quick Start

Installation

pip install langswarm openai
export OPENAI_API_KEY="your-api-key-here"

Note: LangSwarm supports both V1 (archived) and V2 (current) versions with full backward compatibility. See V1/V2 Import Guide for details on using either version.

Simple Agent (30 seconds)

import asyncio
from langswarm import create_agent

async def main():
    # Create an agent
    agent = create_agent(model="gpt-3.5-turbo")
    
    # Chat with it
    response = await agent.chat("What's the capital of France?")
    print(response)

asyncio.run(main())

Multi-Agent Orchestration (Real Power)

from langswarm.core.agents import create_openai_agent, register_agent
from langswarm.core.workflows import create_simple_workflow, get_workflow_engine

# Create specialized agents
researcher = create_openai_agent(
    name="researcher",
    system_prompt="You are a research specialist. Gather comprehensive information."
)

writer = create_openai_agent(
    name="writer", 
    system_prompt="You are a writing specialist. Create clear, engaging content."
)

# Register for orchestration
register_agent(researcher)
register_agent(writer)

# Create workflow: researcher โ†’ writer
workflow = create_simple_workflow(
    workflow_id="content_creation",
    name="Research and Write",
    agent_chain=["researcher", "writer"]
)

# Execute orchestrated workflow
engine = get_workflow_engine()
result = await engine.execute_workflow(
    workflow=workflow,
    input_data={"input": "Write an article about AI agents"}
)

print(result.output)  # Final result from both agents working together

๐Ÿง  Core Concepts

1. Agents

Agents are AI-powered entities with specific roles and capabilities. LangSwarm supports:

  • Multiple providers: OpenAI, Anthropic (Claude), Google (Gemini), Mistral, Cohere, local models
  • Flexible configuration: System prompts, temperature, tools, memory
  • Built-in capabilities: Streaming, structured outputs, cost tracking
# Simple agent creation
agent = create_agent(model="gpt-4", memory=True)

# Advanced agent with tools
agent = create_openai_agent(
    name="assistant",
    model="gpt-4",
    system_prompt="You are a helpful assistant",
    tools=["filesystem", "web_search"]
)

2. Workflows

Workflows define how agents collaborate:

  • Sequential: Agent A โ†’ Agent B โ†’ Agent C
  • Parallel: Multiple agents work simultaneously
  • Conditional: Route based on results or criteria
  • Nested: Complex multi-stage pipelines
# Simple sequential workflow
workflow = create_simple_workflow("task", "My Task", ["agent1", "agent2"])

# Execute
engine = get_workflow_engine()
result = await engine.execute_workflow(workflow, {"input": "task data"})

4. Memory

LangSwarm's conversational memory system is powered by AgentMem, a standalone package that provides enterprise-grade memory management for AI agents.

Key Features:

  • Session Management: Organize conversations with persistent sessions
  • Multiple Backends: SQLite, Redis, In-Memory, and vector stores
  • Auto-Summarization: Automatic conversation compression when limits reached
  • Token Management: Keep context within model limits
  • LLM Integration: Native OpenAI and Anthropic format support
from langswarm.core.memory import create_memory_manager, Message, MessageRole

# Create memory manager
manager = create_memory_manager("sqlite", db_path="conversations.db")
await manager.backend.connect()

# Create a session
session = await manager.create_session(user_id="user123")

# Add messages
await session.add_message(Message(role=MessageRole.USER, content="Hello"))

AgentMem as Standalone Package:

AgentMem can also be used independently in other projects:

pip install agentmem

See the AgentMem documentation for more details on standalone usage.


๐Ÿ› ๏ธ Tools (MCP)

LangSwarm implements the Model Context Protocol (MCP) for tool integration:

Built-in Tools:

  • filesystem - File operations (read, write, list)
  • web_search - Web search capabilities
  • github - GitHub repository operations
  • sql_database - SQL database access
  • bigquery_vector_search - Semantic search in BigQuery
  • codebase_indexer - Code analysis and understanding
  • workflow_executor - Dynamic workflow execution
  • tasklist - Task management
  • message_queue - Pub/sub message handling
# Agent with tools
agent = create_agent(
    model="gpt-4",
    tools=["filesystem", "web_search"]
)

# Tools are automatically discovered and injected
response = await agent.chat("Find the latest Python news and save it to a file")

4. Memory

Conversation history and context management with multiple backends:

  • SQLite: Zero-config, local development
  • Redis: Fast, distributed caching
  • ChromaDB: Vector embeddings and semantic search
  • BigQuery: Analytics-ready, enterprise scale
  • Elasticsearch: Full-text search and analytics
  • Qdrant: High-performance vector search
  • Pinecone: Managed vector database
# Simple memory
agent = create_agent(model="gpt-3.5-turbo", memory=True)

# Advanced memory configuration
config = {
    "memory": {
        "backend": "chromadb",
        "settings": {
            "persist_directory": "./data/memory",
            "embedding_model": "text-embedding-3-small"
        }
    }
}

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    LangSwarm Framework                  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                         โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚   Agents     โ”‚  โ”‚  Workflows   โ”‚  โ”‚    Tools     โ”‚ โ”‚
โ”‚  โ”‚              โ”‚  โ”‚              โ”‚  โ”‚              โ”‚ โ”‚
โ”‚  โ”‚ โ€ข OpenAI     โ”‚  โ”‚ โ€ข Sequential โ”‚  โ”‚ โ€ข MCP Local  โ”‚ โ”‚
โ”‚  โ”‚ โ€ข Anthropic  โ”‚  โ”‚ โ€ข Parallel   โ”‚  โ”‚ โ€ข MCP Remote โ”‚ โ”‚
โ”‚  โ”‚ โ€ข Google     โ”‚  โ”‚ โ€ข Conditionalโ”‚  โ”‚ โ€ข Built-in   โ”‚ โ”‚
โ”‚  โ”‚ โ€ข Mistral    โ”‚  โ”‚ โ€ข Nested     โ”‚  โ”‚ โ€ข Custom     โ”‚ โ”‚
โ”‚  โ”‚ โ€ข Local      โ”‚  โ”‚              โ”‚  โ”‚              โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚                                                         โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                         โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚  โ”‚           Infrastructure Layer                  โ”‚   โ”‚
โ”‚  โ”‚                                                 โ”‚   โ”‚
โ”‚  โ”‚  Memory    Session      Observability   Config โ”‚   โ”‚
โ”‚  โ”‚  โ€ข SQLite  โ€ข Storage    โ€ข OpenTelemetry โ€ข YAML โ”‚   โ”‚
โ”‚  โ”‚  โ€ข Redis   โ€ข Providers  โ€ข LangSmith     โ€ข JSON โ”‚   โ”‚
โ”‚  โ”‚  โ€ข ChromaDBโ€ข Lifecycle  โ€ข Tracing       โ€ข Code โ”‚   โ”‚
โ”‚  โ”‚  โ€ข BigQuery                                     โ”‚   โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“š Use Cases

Content Creation Pipeline

# Specialized agents
researcher = create_openai_agent(name="researcher", system_prompt="Research topics")
writer = create_openai_agent(name="writer", system_prompt="Write engaging content")
editor = create_openai_agent(name="editor", system_prompt="Edit and polish")

# Register all
for agent in [researcher, writer, editor]:
    register_agent(agent)

# Workflow: research โ†’ write โ†’ edit
workflow = create_simple_workflow("content", "Content Pipeline", 
                                 ["researcher", "writer", "editor"])

# Execute
result = await get_workflow_engine().execute_workflow(
    workflow, {"input": "AI in Healthcare"}
)

Code Analysis & Documentation

# Agent with code analysis tools
coder = create_agent(
    model="gpt-4",
    tools=["codebase_indexer", "filesystem", "github"]
)

# Analyze and document
result = await coder.chat(
    "Analyze the repository, find all API endpoints, and create documentation"
)

Customer Support System

# Multiple agents for different tasks
classifier = create_agent(system_prompt="Classify customer inquiries")
support = create_agent(system_prompt="Provide support answers", tools=["bigquery_vector_search"])
escalation = create_agent(system_prompt="Handle escalations")

# Conditional workflow based on classification
# (See docs for advanced workflow patterns)

๐Ÿ”ง Configuration

Code Configuration

from langswarm import create_agent

agent = create_agent(
    model="gpt-4",
    system_prompt="You are a helpful assistant",
    memory=True,
    tools=["filesystem", "web_search"],
    temperature=0.7,
    stream=False,
    track_costs=True
)

YAML Configuration

# langswarm.yaml
version: "2.0"
project_name: "my-agents"

agents:
  - id: assistant
    model: gpt-4
    provider: openai
    system_prompt: "You are a helpful assistant"
    tools: ["filesystem", "web_search"]
    
  - id: analyst
    model: claude-3-sonnet-20240229
    provider: anthropic
    system_prompt: "You are a data analyst"
    memory_enabled: true

memory:
  backend: chromadb
  settings:
    persist_directory: "./data/memory"

workflows:
  - id: research_task
    workflow: "assistant -> analyst -> user"

Load and use:

from langswarm.core.config import load_config

config = load_config("langswarm.yaml")
agent = config.get_agent("assistant")
response = await agent.chat("Hello!")

๐Ÿš€ Advanced Features

Streaming Responses

agent = create_agent(model="gpt-4")

async for chunk in agent.chat_stream("Tell me a story"):
    print(chunk, end="", flush=True)

Cost Tracking

agent = create_agent(model="gpt-4", track_costs=True)

await agent.chat("Hello!")

stats = agent.get_usage_stats()
print(f"Tokens used: {stats['total_tokens']}")
print(f"Estimated cost: ${stats['estimated_cost']}")

Structured Outputs

from pydantic import BaseModel

class UserInfo(BaseModel):
    name: str
    age: int
    email: str

agent = create_agent(model="gpt-4")
result = await agent.chat(
    "Extract: John Doe, 30 years old, john@example.com",
    response_format=UserInfo
)
# result is a UserInfo instance

Observability (OpenTelemetry)

from langswarm.observability import enable_instrumentation

# Enable tracing
enable_instrumentation(
    service_name="my-agents",
    exporter="jaeger",  # or "otlp", "prometheus"
    endpoint="http://localhost:14268/api/traces"
)

# All agent/workflow operations now traced

๐Ÿ› ๏ธ MCP Tool Development

Create custom tools using the Model Context Protocol:

from langswarm.tools import UnifiedTool
from langswarm.core.errors import ErrorContext

class MyCustomTool(UnifiedTool):
    """Custom tool for specific operations"""
    
    metadata = {
        "name": "My Custom Tool",
        "description": "Does something specific",
        "version": "1.0.0"
    }
    
    async def execute(self, input_data: dict, context: ErrorContext = None) -> dict:
        """Main execution method"""
        operation = input_data.get("operation")
        
        if operation == "do_something":
            result = await self._do_something(input_data)
            return {"success": True, "result": result}
        else:
            return {"success": False, "error": f"Unknown operation: {operation}"}
    
    async def _do_something(self, data: dict):
        # Your tool logic here
        return {"message": "Operation completed"}

# Register and use
from langswarm.tools import ToolRegistry

registry = ToolRegistry()
registry.register_tool(MyCustomTool())

# Now available to agents
agent = create_agent(model="gpt-4", tools=["my_custom_tool"])

๐Ÿ“– Documentation

๐Ÿ“‹ Main Resources

๐Ÿ†• V2 - Hierarchical Planning

๐Ÿ“ฆ V1 - Legacy Support

๐Ÿ”ง Advanced Topics


๐ŸŽฏ Production Deployment

Docker

FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .
CMD ["python", "app.py"]

Environment Variables

# Required
export OPENAI_API_KEY="sk-..."

# Optional providers
export ANTHROPIC_API_KEY="sk-ant-..."
export GOOGLE_API_KEY="..."

# Memory backends
export REDIS_URL="redis://localhost:6379"
export BIGQUERY_PROJECT="my-project"
export CHROMADB_PATH="./data/chromadb"

# Observability
export LANGSMITH_API_KEY="..."
export OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318"

Cloud Deployment

LangSwarm supports deployment to:

  • Google Cloud Platform (Cloud Run, Cloud Functions, GKE)
  • AWS (Lambda, ECS, EKS)
  • Azure (Functions, Container Apps, AKS)

See deployment documentation for platform-specific guides.


๐Ÿงช Testing

# Install dev dependencies
pip install -e .[dev]

# Run tests
pytest tests/

# Run specific test suite
pytest tests/unit/
pytest tests/integration/
pytest tests/e2e/

# Run examples
cd examples/simple
python 01_basic_chat.py

๐Ÿค Contributing

We welcome contributions! See CONTRIBUTING.md for guidelines.

Development Setup

# Clone repository
git clone https://github.com/aekdahl/langswarm.git
cd langswarm

# Install in development mode
pip install -e .[dev]

# Run tests
pytest

# Run examples
cd examples/simple && python test_all_examples.py

๐Ÿ“Š Supported Providers

Provider Status Models Notes
OpenAI โœ… Stable GPT-4, GPT-3.5, etc. Full support, function calling
Anthropic โœ… Stable Claude 3.5, Claude 3 Full support, tool use
Google โœ… Stable Gemini Pro, Gemini Pro Vision Multimodal support
Mistral โœ… Stable Mixtral, Mistral Large Function calling
Cohere โœ… Stable Command R+, Command R RAG capabilities
Hugging Face โœ… Beta Open source models Local & API
Local โœ… Beta Ollama, LocalAI, etc. OpenAI-compatible
Custom โœ… Beta Any OpenAI-compatible API Community template

๐Ÿ› ๏ธ Built-in MCP Tools

Tool Description Status
filesystem File operations (read, write, list) โœ… Stable
web_search Web search capabilities โœ… Stable
github GitHub repository operations โœ… Stable
sql_database SQL database access โœ… Stable
bigquery_vector_search Semantic search in BigQuery โœ… Stable
codebase_indexer Code analysis and search โœ… Stable
workflow_executor Dynamic workflow execution โœ… Stable
tasklist Task management โœ… Stable
message_queue_publisher Publish to message queues โœ… Stable
message_queue_consumer Consume from message queues โœ… Stable
realtime_voice OpenAI Realtime API integration โœ… Beta
daytona_environment Dev environment management โœ… Beta
gcp_environment GCP resource management โœ… Beta
dynamic_forms Dynamic form generation โœ… Beta

๐Ÿ“ License

LangSwarm is MIT licensed. See LICENSE for details.


๐Ÿ™‹ Support


๐ŸŽ‰ Examples

See the examples/simple/ directory for 10 working examples:

  1. Basic Chat - Simple agent conversation
  2. Memory Chat - Agent with conversation memory
  3. Two Agents - Multiple agents working together
  4. Different Models - Using different LLM providers
  5. With Tools - Agents using tools (filesystem, web search)
  6. Workflow - Sequential agent workflows
  7. Web Search - Agent with web search capabilities
  8. Config From File - Loading configuration from YAML
  9. Streaming Response - Real-time streaming responses
  10. Cost Tracking - Tracking token usage and costs

Each example is 10-25 lines of code and fully working.


๐Ÿš€ Quick Links


Built with โค๏ธ by the LangSwarm community

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langswarm-0.0.54.dev100.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langswarm-0.0.54.dev100-py3-none-any.whl (1.4 MB view details)

Uploaded Python 3

File details

Details for the file langswarm-0.0.54.dev100.tar.gz.

File metadata

  • Download URL: langswarm-0.0.54.dev100.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.14 Linux/6.11.0-1018-azure

File hashes

Hashes for langswarm-0.0.54.dev100.tar.gz
Algorithm Hash digest
SHA256 91ad16beeeaa0e114a761a054ec7b3bdf8b89b4dd27901bc7b70c632ff48b329
MD5 deaea1f5c2d550a3c712b89994993410
BLAKE2b-256 62f0eb48569ab8d8fa8613e8703eac46b89ef0be86af597b8741a048c7b12a5a

See more details on using hashes here.

File details

Details for the file langswarm-0.0.54.dev100-py3-none-any.whl.

File metadata

  • Download URL: langswarm-0.0.54.dev100-py3-none-any.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.14 Linux/6.11.0-1018-azure

File hashes

Hashes for langswarm-0.0.54.dev100-py3-none-any.whl
Algorithm Hash digest
SHA256 de4b9e17e8a88ae346ae2842ef0610f8f5ac03b179e3a42a436e2909a2b40794
MD5 84010ecffe04f4bc47cf0f120fe3a77e
BLAKE2b-256 ddc39a62a330bcaac586ec3c018e75ab1376cb8a765ac6d114ef7b889d7aea5d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page