Skip to main content

A lightweight framework for building AI agent systems

Project description

LiteSwarm 🐝

A lightweight, LLM-agnostic framework for building AI agents with dynamic agent switching capabilities. Supports 100+ language models through litellm.

[!WARNING] LiteSwarm is currently in early preview and the API is likely to change as we gather feedback.

If you find any issues or have suggestions, please open an issue in the Issues section.

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
  • 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

All examples below are complete and can be run as is.

Hello World

Here's a minimal example showing how to use LiteSwarm's core functionality:

import asyncio

from liteswarm.core import Swarm
from liteswarm.types import LLM, Agent, Message


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 execute
    swarm = Swarm()
    result = await swarm.execute(
        agent=agent,
        messages=[Message(role="user", content="Hello!")],
    )
    print(result.agent_response.content)


if __name__ == "__main__":
    asyncio.run(main())

Streaming with Agent Switching

This example demonstrates real-time streaming and dynamic agent switching capabilities:

import asyncio

from liteswarm.core import Swarm
from liteswarm.types import LLM, Agent, Message, ToolResult


# Define a tool that can switch to another agent
def switch_to_expert(domain: str) -> ToolResult:
    return ToolResult.switch_agent(
        agent=Agent(
            id=f"{domain}-expert",
            instructions=f"You are a {domain} expert.",
            llm=LLM(
                model="gpt-4o",
                temperature=0.0,
            ),
        ),
        content=f"Switching to {domain} expert",
    )


async def main() -> None:
    # 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.response_chunk.completion
            if completion.delta.content:
                print(completion.delta.content, end="", flush=True)
            if completion.finish_reason == "stop":
                print()

    # Optionally, get execution result from stream
    result = await stream.get_return_value()
    print(result.agent_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.chat import LiteChat
from liteswarm.types import LLM, Agent, SwarmEvent


def handle_event(event: SwarmEvent) -> None:
    if event.type == "agent_response_chunk":
        completion = event.response_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 = LiteChat()

    # 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.5.0},
    year = {2025}
}

License

MIT License - see LICENSE file for details.

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

liteswarm-0.5.0.tar.gz (105.1 kB view details)

Uploaded Source

Built Distribution

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

liteswarm-0.5.0-py3-none-any.whl (126.3 kB view details)

Uploaded Python 3

File details

Details for the file liteswarm-0.5.0.tar.gz.

File metadata

  • Download URL: liteswarm-0.5.0.tar.gz
  • Upload date:
  • Size: 105.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for liteswarm-0.5.0.tar.gz
Algorithm Hash digest
SHA256 d7e1e1436df841c07ee78bb6c7857141fd992bb9d1812fa28a73658cb9cf3b0a
MD5 96349759e472342689baf2d22694cdb6
BLAKE2b-256 0bda26c0de4d69b8187e4b951d6cf91b4d7654dd5531240d405f3996687ff749

See more details on using hashes here.

File details

Details for the file liteswarm-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: liteswarm-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 126.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.11

File hashes

Hashes for liteswarm-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f9ae55c608200bc15cd85778646e606c5a0fbe88cf264bee378c5b0dd574216
MD5 0b05afe4ef43f36eebb963979f408d4c
BLAKE2b-256 ae708cdd08d8242117ae01f3eb3e6bce4a7a76091d1a5fdf65cde59511e6c3b4

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