Lightweight framework for LLM agents with tools, hooks, guardrails, and provider routing
Project description
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
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_batch(
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, DialogueRunner, 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 = DialogueRunner()
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
AgentRunnerand advisory inDialogueRunner.
See:
swarm.hooksswarm.guardrailsswarm.runners.agentswarm.runners.dialogue
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swarm_agents-0.1.2.tar.gz.
File metadata
- Download URL: swarm_agents-0.1.2.tar.gz
- Upload date:
- Size: 76.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b0e04e3d33b002deb502ad28438aea363d6019b6d8f651d15fcad4774adcabc
|
|
| MD5 |
cdc452f4f309d5b603ef2606c3ba850c
|
|
| BLAKE2b-256 |
5ef45bfd6e6b32f1a2a313af514e39ffe4b9584cde168bdca8c344e9c7717a6f
|
Provenance
The following attestation bundles were made for swarm_agents-0.1.2.tar.gz:
Publisher:
publish-pypi.yml on logarith-ms/swarm-agents
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
swarm_agents-0.1.2.tar.gz -
Subject digest:
0b0e04e3d33b002deb502ad28438aea363d6019b6d8f651d15fcad4774adcabc - Sigstore transparency entry: 1342762584
- Sigstore integration time:
-
Permalink:
logarith-ms/swarm-agents@9730767ec1b9053b4025dcad780affa304f86e3b -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/logarith-ms
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@9730767ec1b9053b4025dcad780affa304f86e3b -
Trigger Event:
push
-
Statement type:
File details
Details for the file swarm_agents-0.1.2-py3-none-any.whl.
File metadata
- Download URL: swarm_agents-0.1.2-py3-none-any.whl
- Upload date:
- Size: 61.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9145d1701880a6842521de3c561b1a5ca6a7233c57a2b5c56721da068178930a
|
|
| MD5 |
7d81f3d979c8cded96ba2a7a3d9b8a57
|
|
| BLAKE2b-256 |
a468e830ed9f91e32e1ec819fc2a5fcfe4d61cbd03cbc03dd45285a0cc9a09fa
|
Provenance
The following attestation bundles were made for swarm_agents-0.1.2-py3-none-any.whl:
Publisher:
publish-pypi.yml on logarith-ms/swarm-agents
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
swarm_agents-0.1.2-py3-none-any.whl -
Subject digest:
9145d1701880a6842521de3c561b1a5ca6a7233c57a2b5c56721da068178930a - Sigstore transparency entry: 1342762595
- Sigstore integration time:
-
Permalink:
logarith-ms/swarm-agents@9730767ec1b9053b4025dcad780affa304f86e3b -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/logarith-ms
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@9730767ec1b9053b4025dcad780affa304f86e3b -
Trigger Event:
push
-
Statement type: