---
site: steel.dev
collection: docs
canonical_url: https://steel.dev/docs/integrations/langgraph
markdown_url: https://steel.dev/docs/integrations/langgraph.md
content_signal: ai-input=yes, ai-train=yes, search=yes
title: Build a LangGraph agent on Steel
description: Build a LangGraph agent whose tools read and click in a Steel session by ref, and whose last node returns a typed result.
summary: Build a LangGraph agent whose tools read and click in a Steel session by ref, and whose last node returns a typed result.
type: integration-guide
reviewed: 2026-11-17
prerequisites: []
related:
  - https://steel.dev/docs/integrations/ai-sdk.md
  - https://steel.dev/docs/integrations/openai-agents-sdk.md
  - https://steel.dev/docs/integrations/claude-agent-sdk.md
  - https://steel.dev/docs/integrations/crewai.md
  - https://steel.dev/docs/integrations/pydantic-ai.md
agent_setup: https://setup.steel.dev/install.md
docs_index: https://steel.dev/docs/sitemap.md
---

# Build a LangGraph agent on Steel

LangGraph builds an agent as a graph. Nodes do the work, and edges route between them. To give the agent a browser, you write LangChain tools that send [Commands](https://steel.dev/docs/control/commands) to a Steel session. The graph does not know about Steel. It calls the tools, and the tools read the page as a snapshot and click and type on it by ref. A final node turns the conversation into a typed result with a Pydantic model.

This page uses LangGraph for Python.

## Before you begin

- A Steel API key in the `STEEL_API_KEY` environment variable. See [API keys](https://steel.dev/docs/account/api-keys).
- An Anthropic API key in the `ANTHROPIC_API_KEY` environment variable. Any chat model that LangChain supports also works, with its own `langchain-*` package.
- Python 3.11 or later.

Install the packages. The tools send Commands with the Steel SDK, so you do not install Playwright.

```sh
python3 -m venv .venv
source .venv/bin/activate
pip install steel-sdk langgraph langchain-core langchain-anthropic pydantic
```

## Set up the integration

### Build the agent

This program creates a session, then runs a graph with three nodes: `agent` calls the model, `tools` runs the browser tools, and `format` writes the typed result. It releases the session in `finally`.

```python file=langgraph_steel.py
import asyncio
import os
from typing import Literal, Optional

from langchain_anthropic import ChatAnthropic
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.tools import tool
from langgraph.graph import END, START, MessagesState, StateGraph
from langgraph.prebuilt import ToolNode, tools_condition
from pydantic import BaseModel, Field
from steel import AsyncSteel, omit
from steel.types import CommandParam

steel = AsyncSteel()
session_id = ""
after: Optional[str] = None


async def run(commands: list[CommandParam], mode: Literal["snapshot", "diff"]) -> str:
    global after
    response = await steel.sessions.commands(
        session_id, commands=commands, after=after or omit, observe={"mode": mode}
    )
    after = response.next or after
    lines = []
    if response.page:
        lines.append(f"{response.page.title} ({response.page.url})")
    for result in response.results:
        if not result.ok and result.error:
            lines.append(f"{result.command} failed: {result.error.message}")
    if response.observe:
        lines.append(response.observe.snapshot or response.observe.diff or "")
    return "\n".join(lines)


@tool
async def navigate(url: str) -> str:
    """Open a URL. Returns the page as a snapshot: its text, and a ref such as e5
    for each element that you can act on."""
    return await run([{"command": "navigate", "url": url}], "snapshot")


@tool
async def snapshot() -> str:
    """Read the current page again, as a snapshot with refs."""
    return await run([{"command": "snapshot"}], "snapshot")


@tool
async def click(ref: str) -> str:
    """Click the element with this ref. Returns the lines of the snapshot that changed."""
    return await run([{"command": "click", "target": ref}], "diff")


@tool
async def fill(ref: str, value: str) -> str:
    """Replace the text in the field with this ref. Returns the lines of the snapshot that changed."""
    return await run([{"command": "fill", "target": ref, "value": value}], "diff")


@tool
async def press(key: str) -> str:
    """Press a key, such as Enter, in the element that has focus. Returns the lines
    of the snapshot that changed."""
    return await run([{"command": "press", "key": key}], "diff")


class TodoReport(BaseModel):
    todos: list[str] = Field(description="The title of each todo on the page.")
    completed: list[str] = Field(description="The titles of the todos that are completed.")
    items_left: int = Field(description="The number of todos that are not completed.")


class State(MessagesState):
    report: Optional[TodoReport]


tools = [navigate, snapshot, click, fill, press]
model = ChatAnthropic(model="claude-haiku-5-5", api_key=os.environ["ANTHROPIC_API_KEY"])
agent_model = model.bind_tools(tools)
formatter = model.with_structured_output(TodoReport)

SYSTEM = (
    "You operate a Steel cloud browser via tools. "
    "Call navigate to open a URL. To act on an element, pass its ref from the latest snapshot. "
    "Do not invent data."
)


async def agent_node(state: State) -> dict:
    return {"messages": [await agent_model.ainvoke(state["messages"])]}


async def format_node(state: State) -> dict:
    request = HumanMessage(content="Write the report on the todo list.")
    return {"report": await formatter.ainvoke([*state["messages"], request])}


graph = StateGraph(State)
graph.add_node("agent", agent_node)
graph.add_node("tools", ToolNode(tools))
graph.add_node("format", format_node)
graph.add_edge(START, "agent")
graph.add_conditional_edges("agent", tools_condition, {"tools": "tools", END: "format"})
graph.add_edge("tools", "agent")
graph.add_edge("format", END)
app = graph.compile()


async def main() -> None:
    global session_id
    session = await steel.sessions.create()
    session_id = session.id
    print("Session viewer:", session.session_viewer_url)
    try:
        result = await app.ainvoke(
            {
                "messages": [
                    SystemMessage(content=SYSTEM),
                    HumanMessage(
                        content=(
                            "Open https://demo.playwright.dev/todomvc, add the todos Buy milk and "
                            "Walk the dog, and mark Buy milk as completed."
                        )
                    ),
                ]
            },
            config={"recursion_limit": 40},
        )
        print(result["report"].model_dump_json(indent=2))
    finally:
        await steel.sessions.release(session.id)


asyncio.run(main())
```

### Run the program

```sh
python langgraph_steel.py
```

The program prints the [session viewer](https://steel.dev/docs/observability/live-view#open-the-live-view) URL, then the report. Open the session viewer to watch the agent add the todos and check one.

```text nocopy
Session viewer: https://app.steel.dev/sessions/e6c98bdb-a830-4b8a-9e43-bc5cfd596eaa
```

The report is a `TodoReport` as JSON. `todos` has **Buy milk** and **Walk the dog**, `completed` has **Buy milk**, and `items_left` is `1`.

### How the graph uses the session

- `tools_condition` sends the run to `tools` while the model asks for a tool, and to `format` when it answers with text. The edge from `tools` back to `agent` makes the loop. `recursion_limit` caps the number of steps.
- The `format` node reads every message, including the tool results, so it fills `TodoReport` without another visit to the page. It adds a final message that asks for the report, because the current Claude models reject a request that ends with the model's own message.
- Each tool sends one request with `sessions.commands`, and gets the snapshot or the diff back in the same response. `click`, `fill` and `press` return only the lines that changed, so the conversation that the `format` node reads stays short. A failed command, such as a click on a ref that left the page, comes back as text, and the model can take a new snapshot.
- The program creates one session before the graph runs, and the tools share its ID through a module variable. They also keep the `next` of the last response, and send it as `after`, so that a retried request runs its commands once. See [Build your own agent harness](https://steel.dev/docs/control/commands#build-your-own-agent-harness). To run several graphs at the same time, give each run its own session.
- To give the agent more actions, write another `@tool` function that sends a command such as `select`, `scroll` or `wait`, and add it to `tools`. `bind_tools` and `ToolNode` both read that list. [Commands reference](https://steel.dev/docs/reference/commands) lists each command and its fields.

## Next steps

- [LangGraph documentation](https://docs.langchain.com/oss/python/langgraph/overview): Graphs, prebuilt nodes, checkpoints and streaming.
- [LangChain tools](https://docs.langchain.com/oss/python/langchain/tools): How `@tool` turns a function into a tool.
- [Cookbook starter](https://github.com/steel-dev/steel-cookbook/tree/main/examples/langgraph): An agent that extracts trending repositories from GitHub into a typed report.
