LiteSwarm 🐝
A lightweight, LLM-agnostic framework for building AI agents with dynamic agent switching. Supports 100+ language models through litellm.
Features
- Lightweight Core: Minimal base implementation that's easy to understand and extend
- LLM Agnostic: Support for OpenAI, Anthropic, Google, and many more through litellm
- Dynamic Agent Switching: Switch between specialized agents during execution
- Type-Safe Context: Full type safety for context parameters and outputs
- Stateful Chat Interface: Build chat applications with built-in state management
- Event Streaming: Real-time streaming of agent responses and tool calls
Installation
pip install liteswarm
Requirements
-
Python: Version 3.11 or higher
-
Async Runtime: LiteSwarm provides only async API, so you need to use an event loop to run it
-
LLM Provider Key: You'll need an API key from a supported LLM provider (see supported providers)
[click to see how to set keys]
# Environment variable export OPENAI_API_KEY=sk-... os.environ["OPENAI_API_KEY"] = "sk-..." # .env file OPENAI_API_KEY=sk-... # Direct in code LLM(model="gpt-4o", key="sk-...")
Quick Start
Hello World
Here's a minimal example showing how to use LiteSwarm's core functionality:
import asyncio
from liteswarm import LLM, Agent, Message, Swarm
async def main() -> None:
# Create a simple agent
agent = Agent(
id="assistant",
instructions="You are a helpful assistant.",
llm=LLM(model="gpt-4o"),
)
# Create swarm and run
swarm = Swarm()
result = await swarm.run(
agent=agent,
messages=[Message(role="user", content="Hello!")],
)
print(result.final_response.content)
if __name__ == "__main__":
asyncio.run(main())
Streaming with Agent Switching
This example demonstrates real-time streaming and dynamic agent switching:
import asyncio
from liteswarm import LLM, Agent, Message, Swarm, ToolResult, tool_plain
async def main() -> None:
# Define a tool that can switch to another agent
@tool_plain
def switch_to_expert(domain: str) -> ToolResult:
expert_agent = Agent(
id=f"{domain}-expert",
instructions=f"You are a {domain} expert.",
llm=LLM(
model="gpt-4o",
temperature=0.0,
),
)
return ToolResult.switch_to(expert_agent)
# Create a router agent that can switch to experts
router = Agent(
id="router",
instructions="Route questions to appropriate experts.",
llm=LLM(model="gpt-4o"),
tools=[switch_to_expert],
)
# Stream responses in real-time
swarm = Swarm()
stream = swarm.stream(
agent=router,
messages=[Message(role="user", content="Explain quantum physics like I'm 5")],
)
async for event in stream:
if event.type == "agent_response_chunk":
completion = event.chunk.completion
if completion.delta.content:
print(completion.delta.content, end="", flush=True)
if completion.finish_reason == "stop":
print()
# Optionally, get complete run result from stream
result = await stream.get_return_value()
print(result.final_response.content)
if __name__ == "__main__":
asyncio.run(main())
Stateful Chat
Here's how to build a stateful chat application that maintains conversation history:
import asyncio
from liteswarm import LLM, Agent, SwarmChat, SwarmEvent
def handle_event(event: SwarmEvent) -> None:
if event.type == "agent_response_chunk":
completion = event.chunk.completion
if completion.delta.content:
print(completion.delta.content, end="", flush=True)
if completion.finish_reason == "stop":
print()
async def main() -> None:
# Create an agent
agent = Agent(
id="assistant",
instructions="You are a helpful assistant. Provide short answers.",
llm=LLM(model="gpt-4o"),
)
# Create stateful chat
chat = SwarmChat()
# First message
print("First message:")
async for event in chat.send_message("Tell me about Python", agent=agent):
handle_event(event)
# Second message - chat remembers the context
print("\nSecond message:")
async for event in chat.send_message("What are its key features?", agent=agent):
handle_event(event)
# Access conversation history
messages = await chat.get_messages()
print(f"\nMessages in history: {len(messages)}")
if __name__ == "__main__":
asyncio.run(main())
For more examples, check out the examples directory. To learn more about advanced features and API details, see our documentation.
Documentation
Citation
If you use LiteSwarm in your research, please cite our work:
@software{Mozharovskii_LiteSwarm_2025,
author = {Mozharovskii, Evgenii and {GlyphyAI}},
license = {MIT},
month = jan,
title = {{LiteSwarm}},
url = {https://github.com/glyphyai/liteswarm},
version = {0.6.0},
year = {2025}
}
License
MIT License - see LICENSE file for details.
Metadata
Release files for liteswarm 0.6.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 | Provenance | Actions |
|---|---|---|---|---|
| liteswarm-0.6.0.tar.gz | 85.4 kB | Without provenance | Details for liteswarm-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| liteswarm-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for liteswarm-0.6.0-py3-none-any.whl |
Total release size: 185.5 kB
Release files / liteswarm-0.6.0.tar.gz
| Download URL | liteswarm-0.6.0.tar.gz |
|---|---|
| Size | 85.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d069683726f3cb56943513d1523fc14e28320a1686dec22282f3c5720f36d077
|
|
BLAKE2b-256 checksum How to use checksums |
bda5fb34d44efa596d68acadf158706d1f053f20015772d365079a83b22e15c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Release files / liteswarm-0.6.0-py3-none-any.whl
| Download URL | liteswarm-0.6.0-py3-none-any.whl |
|---|---|
| Size | 100.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4b784a9c2034e9b009f064554bbda5bf670274a82418f805794e30015b195fa8
|
|
BLAKE2b-256 checksum How to use checksums |
0509ab157ee31f266f85221ede17a501e85ced9428a695d1d4f8f1fd62f41355
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.11
|
Security for liteswarm 0.6.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.