Integrate Swarms with multiple LLM providers including OpenAI, Anthropic, Groq, and more
Swarms supports a wide range of LLM providers, giving you the flexibility to choose the best model for your use case. The framework provides a unified interface that works seamlessly across all supported providers.
Switch models dynamically based on task requirements:
from swarms import Agentdef get_agent_for_task(task_type: str) -> Agent: """Select the best model for the task type.""" if task_type == "coding": # Use Claude for coding tasks return Agent( agent_name="Coding-Agent", model_name="claude-3-opus-20240229", max_loops=1, ) elif task_type == "analysis": # Use GPT-4 for analysis return Agent( agent_name="Analysis-Agent", model_name="gpt-4", max_loops=1, ) elif task_type == "fast": # Use Groq for speed return Agent( agent_name="Fast-Agent", model_name="groq/llama-3.1-70b-versatile", max_loops=1, ) else: # Default to GPT-4o Mini return Agent( agent_name="Default-Agent", model_name="gpt-4o-mini", max_loops=1, )# Use the appropriate agentagent = get_agent_for_task("coding")result = agent.run("Write a Python function to sort a list")
from swarms import Agent, SequentialWorkflow# Research with Claude (good at analysis)researcher = Agent( agent_name="Researcher", model_name="claude-3-opus-20240229", system_prompt="Research the topic thoroughly.",)# Write with GPT-4 (good at creative writing)writer = Agent( agent_name="Writer", model_name="gpt-4", system_prompt="Write an engaging article.",)# Fast review with Groqreviewer = Agent( agent_name="Reviewer", model_name="groq/llama-3.1-70b-versatile", system_prompt="Review and provide feedback.",)workflow = SequentialWorkflow( agents=[researcher, writer, reviewer])result = workflow.run("The future of AI")
Optimize costs by using the right model for each task:
from swarms import Agent# Use cheaper models for simple taskssimple_agent = Agent( agent_name="Simple-Task-Agent", model_name="gpt-4o-mini", # Most cost-effective max_loops=1,)# Use premium models only when neededcomplex_agent = Agent( agent_name="Complex-Task-Agent", model_name="claude-3-opus-20240229", # Most capable max_loops=1,)