Back to Home
Financial Agents

Comparing 3 Agent Frameworks: DSPy vs Swarm

AI-assisted — drafted with AI, reviewed by editors

James Thornton

Former hedge fund analyst. Writes about AI-driven investment tools.

May 21, 20268 min read

# Comparing 3 Agent Frameworks: DSPy vs Swarm vs LangGraph ## 1. What each framework does and who it’s for **DSPy** treats prompting as programmable code. You declare a *signature* (input → output t...

Comparing 3 Agent Frameworks: DSPy vs Swarm vs LangGraph

1. What each framework does and who it’s for

DSPy treats prompting as programmable code. You declare a signature (input → output types) and wrap it in a module that can be compiled with a language model to produce reliable predictions. It targets developers who want to version, test, and optimize prompts like ordinary software.

Swarm is an OpenAI‑provided lightweight library for orchestrating multiple agents that can hand off work to one another. Each agent is a simple function with a name, instructions, and optional tools. Swarm fits teams that need a quick way to build agent networks without heavyweight graph engines.

LangGraph extends LangChain with a directed‑graph execution model. Nodes are LangChain runnable objects (LLMs, tools, custom functions); edges define control flow, including loops and conditional branches. It is aimed at engineers building complex, stateful LLM applications where the control logic cannot be expressed as a simple chain.

2. Key features and capabilities

Feature DSPy Swarm LangGraph
Core abstraction Signature → Module → Optimizer Agent (name, instructions, tools) + handoff Node (Runnable) + Edge (control flow)
Prompt management Declarative signatures; automatic few‑shot example generation Inline instructions per agent Prompt templates inside nodes; can be versioned
Optimization Built‑in teleprompters (e.g., BootstrapFewShot, MIPRO) for few‑shot and metric‑driven tuning None; relies on manual prompt engineering None; optimization left to user or LangChain callbacks
Tool use Modules can declare tool fields; executed at runtime Agents can be given a list of tool callables Tools are LangChain tools attached to nodes
State handling Implicit via module parameters; no built‑in memory No shared state; each agent stateless unless you pass context Explicit state object passed between nodes; supports checkpoints
Deployment Export as Python callable; can be served with FastAPI, etc. Same; lightweight import Export as LangChain Runnable; compatible with LangServe
Community & docs Stanford DSPy repo, tutorials, paper (ACL 2023) OpenAI Swarm repo, quickstart guide LangChain docs, LangGraph section

3. Architecture and how it works

DSPy

A DSPy program consists of:

  1. Signature – a type‑annotated class describing inputs and outputs (e.g., question -> answer).
  2. Module – a callable that binds a signature to a language model (e.g., dspy.Predict, dspy.ChainOfThought).
  3. Optimizer – a teleprompter that trains the module on a dataset to improve predictions.

At compile time, the optimizer calls the LM with few‑shot examples generated from the training set, updates the module’s internal prompt, and returns a ready‑to‑use predictor. The resulting object is a plain Python function; no special runtime is needed.

Swarm

Swarm’s core is the Agent class:

from swarm import Agent

def sales_agent():
    return Agent(
        name="Sales",
        instructions="You help users find products.",
        tools=[search_catalog]
    )

Agents are linked via handoffs: an agent can return a special Handoff object naming the next agent. The runtime loops, invoking the current agent, passing its output as input to the next, until a terminal agent returns a final answer. No persistent graph is stored; the flow is defined by the sequence of handoffs returned at runtime.

LangGraph

LangGraph builds a StateGraph where:

  • State is a TypedDict shared across nodes.
  • Node is any LangChain Runnable (LLM, tool, custom function).
  • Edge defines the next node(s); can be conditional (add_conditional_edges) or permanent.

Execution starts at a defined entry node, runs its runnable, updates the state, then follows edges. Loops are possible by pointing an edge back to an earlier node. The graph can be compiled and invoked like a regular Runnable, enabling integration with LangServe for API serving.

4. Real-world use cases

DSPy

  • Question answering over internal docs – Define a signature question -> answer, attach a retrieval module, and use MIPRO to optimize few‑shot prompts for accuracy on a validation set.
  • SQL generation – Signature natural_language -> sql_query; optimize with execution‑feedback metric to improve correctness.

Swarm

  • Customer‑support triage – Agent A greets user and classifies issue; hands off to Agent B (billing) or Agent C (technical) based on classification; each agent has access to relevant tools (e.g., refund API, knowledge base search).
  • Interactive storytelling – One agent drives plot, another controls character dialogue, handoffs triggered by user choices.

LangGraph

  • Multi‑step research assistant – Node 1: generate sub‑questions; Node 2: retrieve passages for each; Node 3: synthesize answer; conditional edges re‑run retrieval if confidence low.
  • Agent‑based workflow with memory – State includes chat_history and user_profile; nodes modify these fields, enabling context‑aware conversations across turns.

5. Strengths and limitations

Framework Strengths Limitations
DSPy - Treats prompts as code, enabling version control and testing.
- Automatic few‑shot example generation reduces manual prompt engineering.
- Optimizers can significantly boost accuracy on downstream tasks.
- Learning curve for signature‑module‑optimizer pattern.
- Debugging optimized prompts can be opaque.
- Less suited for highly dynamic, multi‑agent interactions out of the box.
Swarm - Minimal boilerplate; agents are simple functions.
- Easy to prototype handoff‑based workflows.
- No external state management required for simple cases.
- No built‑in optimization or few‑shot generation.
- Scaling to complex conditional logic requires manual handoff routing.
- Limited tool integration compared to LangChain ecosystem.
LangGraph - Full control flow (loops, conditionals, parallelism) via graph.
- Integrated with LangChain’s rich tool and memory ecosystem.
- State persistence and checkpoints support long‑running workflows.
- More verbose setup (defining State, nodes, edges).
- Overhead for simple linear chains where a LangChain LLMChain would suffice.
- Requires familiarity with LangChain concepts.

6. How they compare to alternatives

Criterion DSPy Swarm LangGraph LangChain (LLMChain) AutoGen
Declarative prompt programming ✘ (uses templates)
Built‑in prompt optimization
Multi‑agent handoffs ✘ (single module) ✔ (via graph edges) ✘ (needs custom logic) ✔ (agent conversation)
Graph‑based control flow
Tool ecosystem depth Moderate (via DSPy tools) Light (custom callables) Deep (LangChain tools) Deep Moderate (AutoGen tools)
State persistence Limited (module params) None ✔ (state object) None (unless added) ✔ (shared memory)
Typical use case Prompt‑centric tasks Simple agent networks Complex stateful workflows Straight chains/LLMs Multi‑agent problem solving

7. Getting started guide

DSPy

# Install the latest release from PyPI
pip install dspy-ai

# Quick example: optimize a QA module
import dspy

# 1. Define a signature
class QA(dspy.Signature):
    question = dspy.InputField()
    answer   = dspy.OutputField()

# 2. Create a Predict module
qa = dspy.Predict(QA)

# 3. Provide a tiny trainset (optional but shows optimization)
trainset = [
    dspy.Example(question="What is the capital of France?", answer="Paris").with_inputs('question'),
    dspy.Example(question="Who wrote 1984?", answer="George Orwell")
]

# 4. Optimize with BootstrapFewShot (uses the LM to generate examples)
optimizer = dspy.BootstrapFewShot(metric=dspy.evaluate.answer_exact_match)
optimized_qa = optimizer.compile(qa, trainset=trainset)

# 5. Use it
print(optimized_qa(question="What is the largest planet?"))

Swarm

pip install swarm

from swarm import Agent, Handoff, Runner

def greet():
    return Agent(
        name="Greeter",
        instructions="Say hello and ask how you can help.",
    )

def billing():
    return Agent(
        name="Billing",
        instructions="Handle refund requests.",
        tools=[refund_tool]
    )

def route(message):
    if "refund" in message.lower():
        return Handoff(target=billing())
    return None

greeter = greet()
greeter.handlers = [route]  # pseudo‑code; Swarm uses return values

runner = Runner()
result = runner.start(greeter, "I want a refund for my last order.")
print(result)

LangGraph

pip install langgraph

from typing import TypedDict
from langgraph.graph import StateGraph, END
from langchain_openai import ChatOpenAI

class AgentState(TypedDict):
    topic: str
    summary: str

llm = ChatOpenAI(temperature=0)

def research_node(state):
    # pretend we call a search tool
    return {"summary": f"Research on {state['topic']} completed."}

def write_node(state):
    return {"summary": llm.predict(f"Write a short summary: {state['summary']}")}

builder = StateGraph(AgentState)
builder.add_node("research", research_node)
builder.add_node("write", write_node)
builder.set_entry_point("research")
builder.add_edge("research", "write")
builder.add_edge("write", END)

graph = builder.compile()

result = graph.invoke({"topic": "renewable energy trends 2024"})
print(result["summary"])

8. Closing thoughts

DSPy shines when the primary challenge is crafting reliable prompts; its optimizers turn a vague instruction into a tuned predictor with minimal manual effort. Swarm offers the fastest path to a network of agents that simply pass work along, making it ideal for prototyping or scenarios where the agent logic is essentially a decision tree. LangGraph provides the most expressive control flow, letting you embed loops, conditionals, and shared state—features essential for production‑grade assistants that need to reason over multiple steps and retain context. Choose the framework that matches the complexity of your workflow: DSPy for prompt engineering, Swarm for lightweight handoff chains, LangGraph for full‑featured graph‑based orchestration.

Keywords

DSPySwarmLangGraphagent frameworksprompt optimizationmulti-agent orchestrationLangChaingetting started

Keep reading

More from DriftSeas on AI agents and the tools around them.