Swarm Agents
swarm-agents is a standalone Python package for building LLM agents with:
- provider-agnostic model routing
- function/tool execution
- lifecycle hooks
- input/output guardrails
- streaming dialogue execution
- autonomous batch execution
The import path is swarm.
Package Docs
- Architecture:
docs/architecture.md - Error handling:
docs/error-handling.md - Durable runtime:
docs/runtime.md
Releases
Versions are derived from Git tags. A pushed vX.Y.Z tag creates:
- a GitHub Release with generated notes and attached build artifacts
- a PyPI publish job via Trusted Publishing
Install
Base package:
pip install swarm-agents
With optional image helpers:
pip install "swarm-agents[image]"
From Git:
pip install "swarm-agents[dev] @ git+ssh://git@github.com/logarith-ms/swarm-agents.git@vX.Y.Z"
PyPI is the primary distribution channel. Git installs remain useful for pinned previews or local validation.
Quick Start
Swarm is the beginner-facing entry point. It handles provider configuration and model-router wiring for you.
from swarm import Agent, Swarm
client = Swarm() # loads config from environment via ConfigLoader.load()
agent = Agent(
name="Assistant",
model="openai/gpt-4o",
instructions="You are a helpful assistant.",
)
async for chunk in client.run_streamed(
agent,
deps=your_context,
stream_manager=your_stream_manager,
message_store=your_message_store,
retry_policy=your_retry_policy,
max_turns=10,
):
print(chunk)
Batch execution:
from swarm import Agent, Swarm
client = Swarm(
config={
"openai_api_key": "...",
"openai_organization": "...",
"openai_project": "...",
"anthropic_api_key": "...",
"google_api_key": "...",
"vertex_project": "...",
"vertex_location": "us-east1",
}
)
agent = Agent(
name="Planner",
model="openai/gpt-4o",
instructions="Work through the task step by step.",
)
result = await client.run(
agent,
deps=your_context,
stream_manager=your_stream_manager,
message_store=your_message_store,
retry_policy=your_retry_policy,
max_turns=5,
)
print(result.signal)
What The Facade Does
Swarm(...) removes only the provider/router wiring:
ConfigLoader.load()ProviderFactory(config=...)ModelRouter(factory=...)RunContext(..., router=...)
You still provide implementations for the runtime protocols:
ContextProtocolStreamManagerProtocolMessageStoreProtocol- optionally
RetryPolicyProtocol
Those implementations are application-specific and are intentionally not bundled as defaults in this package.
Model Fallback
Agent.model supports either:
- a single model string
- a list of models in fallback order
Example:
from swarm import Agent
agent = Agent(
name="FallbackAgent",
model=[
"openai/gpt-4o",
"anthropic/claude-3-5-sonnet",
"google/gemini-2.0-flash",
],
instructions="Try providers in order until one succeeds.",
)
The router will try each model in sequence until one succeeds or all fail.
Provider Configuration
ConfigLoader.load() reads these environment variables and maps them to the config keys expected by ProviderFactory:
OPENAI_API_KEYOPENAI_ORGANIZATIONOPENAI_PROJECTANTHROPIC_API_KEYGOOGLE_API_KEYVERTEX_PROJECT_ID->vertex_projectVERTEX_LOCATION->vertex_location
Advanced / Explicit Wiring
If you want full control, keep using the manual path directly:
from swarm import Agent, ConfigLoader, Runner, ModelRouter, ProviderFactory
from swarm.context import RunContext
config = ConfigLoader.load()
router = ModelRouter(factory=ProviderFactory(config=config))
agent = Agent(
name="Assistant",
model="openai/gpt-4o",
instructions="You are a helpful assistant.",
)
run_context = RunContext(
deps=your_context,
stream_manager=your_stream_manager,
message_store=your_message_store,
router=router,
retry_policy=your_retry_policy,
)
runner = Runner()
async for chunk in runner.run_streamed(agent=agent, run_context=run_context, max_turns=10):
print(chunk)
Optional Image Support
swarm.util.image_to_base64(...) is available behind the image extra.
If you call it without installing the extra, it raises an ImportError with the install command:
pip install "swarm-agents[image]"
Hooks And Guardrails
- Hooks are observational and never alter execution flow.
- Input guardrails run before the first LLM call.
- Output guardrails are blocking in
Runnerand advisory inRunner.
See:
swarm.hooksswarm.guardrailsswarm.runners.runnerswarm.runners.runner
Development
pip install -e ".[dev]"
ruff format --check .
ruff check .
mypy swarm
pytest tests -q
python -m build
Maintainer Release Flow
- Merge conventional commits to
main. - Wait for
CIto pass andAuto Tagto create the next semanticvX.Y.Ztag. - The pushed tag triggers both:
Releasefor the GitHub Release page and attached wheel/sdistPublish PyPIfor Trusted Publishing to PyPI
- Verify the new version on:
https://github.com/logarith-ms/swarm-agents/releaseshttps://pypi.org/project/swarm-agents/
Notes
swarm_task/is intentionally not part of this package.swarm/ERROR_HANDLING.mdis intentionally not promoted into the package root.
Metadata
Release files for swarm-agents 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| swarm_agents-0.4.0.tar.gz | 168.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| swarm_agents-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 286.1 kB
Release files / swarm_agents-0.4.0.tar.gz
| Download URL | swarm_agents-0.4.0.tar.gz |
|---|---|
| Size | 168.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ea4663ecee9807de762130cbfc0223e65943784f60b914d43b1b9fcc254cc4c9
|
|
BLAKE2b-256 checksum How to use checksums |
45c6c3c3ad6493b93e96c3cf53f65272d3094a6db43a69439e56762dcb7e6377
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.
Transparency logRelease files / swarm_agents-0.4.0-py3-none-any.whl
| Download URL | swarm_agents-0.4.0-py3-none-any.whl |
|---|---|
| Size | 117.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8983df4472098e7614378fd225721addfb4f7b80c702aa4aad3dc9f049c6c555
|
|
BLAKE2b-256 checksum How to use checksums |
cfe8b2288752e872f960ae12b73ba0287ca906b02289c8b659b5e9df4c487d81
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.
Transparency log