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 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_KEYenvironment variable. See API keys. - An Anthropic API key in the
ANTHROPIC_API_KEYenvironment variable. Any chat model that LangChain supports also works, with its ownlangchain-*package. - Python 3.11 or later.
Install the packages. The tools send Commands with the Steel SDK, so you do not install Playwright.
python3 -m venv .venv
source .venv/bin/activate
pip install steel-sdk langgraph langchain-core langchain-anthropic pydanticSet 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.
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
python langgraph_steel.pyThe program prints the session viewer URL, then the report. Open the session viewer to watch the agent add the todos and check one.
Session viewer: https://app.steel.dev/sessions/e6c98bdb-a830-4b8a-9e43-bc5cfd596eaaThe 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_conditionsends the run totoolswhile the model asks for a tool, and toformatwhen it answers with text. The edge fromtoolsback toagentmakes the loop.recursion_limitcaps the number of steps.- The
formatnode reads every message, including the tool results, so it fillsTodoReportwithout 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,fillandpressreturn only the lines that changed, so the conversation that theformatnode 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
nextof the last response, and send it asafter, so that a retried request runs its commands once. See 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
@toolfunction that sends a command such asselect,scrollorwait, and add it totools.bind_toolsandToolNodeboth read that list. Commands reference lists each command and its fields.