Key Moments

DSPy vs. LangGraph: AI Agent Frameworks Explained | Stanford CS329Z Lecture 5 with Michael Ryan

Stanford OnlineStanford Online
Education5 min read78 min video
Nov 13, 2026|7,925 views|89|2
Save to Pod
TL;DR

New AI agent frameworks like DSPy and LangGraph offer powerful abstractions, but understanding their underlying mechanics is crucial for robust engineering.

Key Insights

1

Engineering AI systems requires three pillars: the system itself, data, and evaluation metrics, not just building the core model.

2

DSPy focuses on signatures (input/output contracts), modules (composing logic), and optimizers (prompt tuning), enabling structured and improvable agent code.

3

LangGraph uses a stateful graph approach with nodes and edges to orchestrate complex workflows, offering strong control flow and conditional logic.

4

Agent harnesses like MiniSweet Agent, OpenHands, and others offer pre-built agent structures, with design choices in context management, tool exposure, and memory impacting performance.

5

Reliability and observability tools like Temporal, MLflow, and LangSmith are essential for debugging and managing long-running AI agent processes, drawing parallels from software engineering.

6

Best practices for agent engineering include starting simple, designing for failure and recovery, running rigorous evaluations with domain-specific metrics, and understanding the underlying system abstractions.

The three pillars of AI system engineering

Engineering AI systems involves three fundamental components: the system itself (language models, agents), the data (training data, feedback), and robust evaluation metrics (accuracy, pass rate, LLM judges). While it's possible to quickly assemble a functional agent, true engineering requires a holistic approach that integrates all three pillars. This lecture emphasizes that frameworks aim to simplify the system-building aspect, but data and evaluations remain critical for developing effective AI.

DSPy: Signatures, Modules, and Optimization for Structured Agents

DSPy is a framework designed to make writing and optimizing AI agent code more manageable. Its core concepts include signatures, which define strict input and output contracts for language models, ensuring structured data exchange. Modules compose these signatures with Python logic, allowing for complex operations like chain-of-thought reasoning or selecting the best of multiple outputs. A key feature is the optimizer, which can automatically improve prompts and weights based on provided data and metrics, effectively tuning the agent for better performance. For instance, an optimizer can take a basic instruction like 'extract event details from email' and refine it into a highly effective prompt that achieves 100% accuracy on a test set, even for smaller models like Gemma 312B. DSPy also facilitates building agents by integrating architectures like ReAct and allowing the specification of tools, simplifying agent creation to just a few lines of code. Recursive language models can also be leveraged within DSPy to handle very long contexts, processing large amounts of data like an entire inbox to answer specific questions.

LangGraph: Orchestrating Agents with State, Nodes, and Edges

LangGraph provides a framework for building stateful, graph-based agent workflows. The core idea is a shared 'state' object that nodes (functions) read from and write to, guiding the execution flow. Edges define the transitions between nodes, and these can be conditional, allowing for dynamic routing based on the state. For example, a graph can extract event details from an email (state), and then conditionally route to an emoji-picking node if details are found, or go directly to the end if not. LangGraph also supports building agents by defining an LLM and its available tools as nodes in the graph. This approach offers robust control flow, ease of debugging through visualization, and the potential for distributed computing, where different nodes can run on separate machines. A significant advantage is the ability to assign different models to different nodes within the graph, providing fine-grained control over resource utilization and model selection.

Agent Harnesses: Pre-built Frameworks for Agent Development

Agent harnesses are pre-built structures that provide the foundational components for an agent, abstracting away common implementation details like memory management, tool integration, and context handling. Examples include MiniSweet Agent, which offers a minimalist approach with basic Bash tools and simple linear context stacking; OpenHands, which uses a more complex event-based context management; and widely-used platforms like Claude Code. These harnesses make different trade-offs regarding tool sets, memory persistence, execution environments (e.g., starting new subprocesses vs. persistent terminals), and error handling. The choice of harness can significantly impact an agent's success rate on benchmarks like Terminal Bench, highlighting the importance of selecting or designing a harness that aligns with the specific use case and desired performance characteristics.

Reliability and Observability for Robust Agent Systems

As AI agents handle increasingly complex and long-running tasks, ensuring their reliability and understanding their behavior becomes critical. Orchestration tools like Temporal enable the creation of durable workflows by linking potentially fallible 'activities' (functional units) and managing their execution, retries, and state persistence. Observability frameworks such as MLflow, LangSmith, and LangFuse provide vital insights into agent operations, logging inputs, outputs, token usage, latency, and errors. This allows developers to debug failures, monitor system health, and assess cost-effectiveness. Lessons from traditional software engineering, like implementing timeouts, retry limits, and comprehensive logging, directly translate to agent engineering, enabling more robust and manageable AI systems.

Best practices for building and evaluating AI agents

Effective AI agent engineering follows a cyclical process of building, running, evaluating, and improving. Key best practices include starting with simple workflows before resorting to complex agents, carefully selecting and managing the tools exposed to the agent, and designing for failure with observable and recoverable mechanisms. Rigorous, domain-specific evaluations are paramount for making informed design decisions and optimizing performance; focusing on custom metrics relevant to the agent's purpose is more effective than chasing headline benchmarks. Developers should embrace abstractions offered by frameworks like DSPy and LangGraph but also strive to understand the underlying system mechanics for deeper problem-solving. Finally, meticulous monitoring and logging, coupled with well-defined evaluation metrics, are essential for building and maintaining high-quality, reliable AI systems.

Common Questions

Observability tools surface issues and errors to you for debugging, while reliability tools automatically handle failures, such as restarting crashed systems to ensure continuous operation.

Topics

Mentioned in this video

Software & Apps
Open-Claw

A mainstream agent that operates on a user's machine, wakes itself up periodically, and builds identity from markdown files.

LangSmith

An observability tool for the LangGraph ecosystem, used for monitoring token usage, latency, and evaluation scores.

Shepherd

A research example of a meta-agent that provides an observability layer for other agents.

Gemma 312B

A smaller open-weight model used to demonstrate the effectiveness of DSPy optimizers in improving performance.

OpenHands

An agent harness that rebuilds context from events and manages persistent terminals.

Claude

Mentioned as having built a calendar application for the demo, and used in the context of Claude Code.

LangChain

A framework related to LangGraph, used for initializing LLMs and creating agents.

LangFuse

An observability tool for monitoring agent workflows, tracking errors, and analyzing traces.

Pi Agent

A simple agent harness with limited tool calls and no permission system.

Claude Code

An agent system that can be used with a subscription and interacts with the user's environment.

LangGraph

A framework for building agent workflows using a graph-based approach with shared state, nodes, and edges. It is presented as an alternative and complementary tool to DSPy.

PyTorch

A machine learning framework used for writing backpropagation by hand, abstracting away underlying system details.

MLflow

An observability tool mentioned alongside LangFuse for monitoring and debugging agent systems.

GPT-5.4 nano

The language model used in the initial DSPy demonstration for basic chat functionality.

NumPy

A Python library used in conjunction with Python for writing backpropagation by hand.

GPT-3.5 Turbo

The 'teacher model' specified for the DSPy optimizer.

Sto

An agent harness that stores context history as a tree structure.

DSPY

An open-source framework for building AI agent workflows, known for its signatures, modules, and optimizers. It allows for defining input/output contracts and optimizing prompts.

Mini Sweet Agent

A minimalist open-source agent harness that uses Bash for tools and a simple linear history for context management.

More from Stanford Online

View all 177 summaries

Ask anything from this episode.

Save it, chat with it, and connect it to Claude or ChatGPT. Get cited answers from the actual content — and build your own knowledge base of every podcast and video you care about.

Get Started Free