Skip to content

Build & run a graph

This guide walks from a scaffold to a running API server.

1. Scaffold and install

langstitch new my-agent
cd my-agent
python -m venv .venv && . .venv/bin/activate
pip install -e ".[server,graph,llm,http]"

2. Describe the agent

Configuration in application.yaml, components via decorators (see Decorators).

app:
  name: my_agent
  version: 0.1.0
model:
  provider: openai
  name: gpt-4o-mini
  temperature: 0.2
server:
  host: 0.0.0.0
  port: 8000
from langstitch import graph, graph_node, GraphBuilder, END

@graph_node(description="Answer the latest message.")
def respond(state: dict) -> dict:
    return {"response": "...", "messages": [...]}

@graph(name="main", entrypoint=True)
def main_graph() -> GraphBuilder:
    g = GraphBuilder("main")
    g.add_node("respond", respond)
    g.set_entry_point("respond")
    g.add_edge("respond", END)
    return g

3. Bootstrap and invoke in code

from langstitch import LangStitchApp

app = LangStitchApp.bootstrap()
graph = app.build_graph()           # compiles to a LangGraph StateGraph
result = app.invoke({"messages": [{"role": "user", "content": "hi"}]})
print(result)

4. Run the server

langstitch run               # or: langstitch run --host 0.0.0.0 --port 8000

The server exposes /health, /info, and /invoke:

curl -s localhost:8000/info | python -m json.tool
curl -s -X POST localhost:8000/invoke \
  -H 'content-type: application/json' \
  -d '{"messages": [{"role": "user", "content": "hi"}]}'

5. Add models, tools, and services

from langstitch import Context, run_llm, get_llm_provider

ctx = Context(data={"question": q}, messages=[{"role": "user", "content": q}])

def call_model(llm_ctx):
    return get_llm_provider().invoke(
        [{"role": "system", "content": llm_ctx.system}] + llm_ctx.messages,
    )

answer = run_llm(ctx, call_model, persona="assistant", tool_tags=["billing"], key="answer")

See Providers and External services for models and HTTP clients.