|
| 1 | +# at130-langgraph — AgentEx Temporal + LangGraph |
| 2 | + |
| 3 | +A starter template for building AI agents with AgentEx, [LangGraph](https://langchain-ai.github.io/langgraph/), |
| 4 | +and Temporal — where **Temporal is the runtime and LangGraph is the agent framework**. |
| 5 | + |
| 6 | +It uses the official [`temporalio.contrib.langgraph`](https://docs.temporal.io/develop/python/integrations/langgraph) |
| 7 | +plugin: each LangGraph node runs either as a durable **Temporal activity** or |
| 8 | +inline in the **workflow**, configured per node with `execute_in`. You get |
| 9 | +per-node durability, automatic retries, and full visibility in the Temporal UI |
| 10 | +— without LangGraph's own runtime or an external checkpoint database. |
| 11 | + |
| 12 | +> The Temporal LangGraph plugin is currently **experimental**; its API may change. |
| 13 | +
|
| 14 | +## What's in the box |
| 15 | + |
| 16 | +- **Nodes as activities** — the LLM (`agent`) node runs as a retried, observable |
| 17 | + Temporal activity; the `tools` node runs in the workflow (see below). |
| 18 | +- **Human-in-the-loop** — approval-gated tools raise a LangGraph `interrupt`; |
| 19 | + the workflow pauses on a Temporal signal (`provide_approval`) until a human |
| 20 | + approves or rejects, then resumes. |
| 21 | +- **Live introspection via Temporal queries** — `get_status`, |
| 22 | + `get_pending_approval`, `get_graph_state`, and `get_graph_mermaid` / |
| 23 | + `get_graph_ascii` to render the agent graph while it runs. |
| 24 | +- **Multi-turn memory** — the running message list is kept on the workflow |
| 25 | + instance, durable for free. |
| 26 | +- **Tracing/observability** — a per-turn span shipped to SGP/AgentEx. |
| 27 | + |
| 28 | +## The agent graph |
| 29 | + |
| 30 | +``` |
| 31 | +START --> agent --> (tool calls?) --> tools --> agent |
| 32 | + --> (no tool calls?) --> END |
| 33 | +``` |
| 34 | + |
| 35 | +`project/graph.py` defines this graph. The `agent` node is marked |
| 36 | +`execute_in="activity"`; the `tools` node is `execute_in="workflow"`. Query |
| 37 | +`get_graph_mermaid` at runtime to see it rendered. |
| 38 | + |
| 39 | +### Why the tools node runs in the workflow |
| 40 | + |
| 41 | +The `tools` node runs inline in the workflow (not as an activity) for two |
| 42 | +reasons: the `AIMessage` with tool calls stays intact without crossing an |
| 43 | +activity boundary, and LangGraph `interrupt` (used for human approval) must run |
| 44 | +where the workflow can pause on a Temporal signal. For long-running or heavily |
| 45 | +side-effecting tools, move that work into its own `execute_in="activity"` node. |
| 46 | +The router and tools are `async` so LangGraph awaits them directly (sync |
| 47 | +callables are offloaded via `run_in_executor`, which Temporal workflows forbid). |
| 48 | + |
| 49 | +## Project structure |
| 50 | + |
| 51 | +``` |
| 52 | +at130_langgraph/ |
| 53 | +├── project/ |
| 54 | +│ ├── __init__.py |
| 55 | +│ ├── acp.py # Thin async ACP server; registers the LangGraphPlugin |
| 56 | +│ ├── workflow.py # Temporal runtime: runs the graph, HIL, queries, memory |
| 57 | +│ ├── graph.py # LangGraph graph; nodes tagged execute_in activity/workflow |
| 58 | +│ ├── tools.py # Async tool definitions + approval set |
| 59 | +│ └── run_worker.py # Temporal worker; registers the LangGraphPlugin |
| 60 | +├── Dockerfile |
| 61 | +├── manifest.yaml |
| 62 | +├── environments.yaml |
| 63 | +├── dev.ipynb |
| 64 | +└── pyproject.toml |
| 65 | +``` |
| 66 | + |
| 67 | +## Running the agent |
| 68 | + |
| 69 | +```bash |
| 70 | +agentex uv sync |
| 71 | +source .venv/bin/activate |
| 72 | + |
| 73 | +# Start the agent (ACP server + Temporal worker) |
| 74 | +agentex agents run --manifest manifest.yaml |
| 75 | +``` |
| 76 | + |
| 77 | +The agent starts on port 8000. Open the Temporal UI at http://localhost:8080 to |
| 78 | +watch workflows and activities execute. Use `dev.ipynb` to create a task and |
| 79 | +send messages. |
| 80 | + |
| 81 | +## Human-in-the-loop |
| 82 | + |
| 83 | +Tools listed in `TOOLS_REQUIRING_APPROVAL` (in `project/tools.py`) raise a |
| 84 | +LangGraph `interrupt` before they run. The workflow surfaces the pending call |
| 85 | +(queryable via `get_pending_approval`) and waits — durably, for as long as it |
| 86 | +takes — for a `provide_approval` signal carrying the decision: |
| 87 | + |
| 88 | +```python |
| 89 | +# decision: {"approved": true, "approver": "daniel", "reason": "looks good"} |
| 90 | +``` |
| 91 | + |
| 92 | +If rejected, the rejection is fed back to the model so it can adjust. |
| 93 | + |
| 94 | +## Adding tools |
| 95 | + |
| 96 | +1. Define an **async** `@tool` function in `project/tools.py` and add it to `TOOLS`. |
| 97 | +2. (Optional) add its name to `TOOLS_REQUIRING_APPROVAL` to gate it behind |
| 98 | + human approval. |
| 99 | + |
| 100 | +The model is bound with `TOOLS` and the tool node looks them up by name, so no |
| 101 | +other wiring is needed. |
| 102 | + |
| 103 | +## Configuration |
| 104 | + |
| 105 | +Tune the model in `project/graph.py` (`MODEL_NAME`) and the system prompt |
| 106 | +(`SYSTEM_PROMPT`). Per-node activity timeouts and retry policies live in the |
| 107 | +node `metadata` in `build_graph()`. |
| 108 | + |
| 109 | +## Environment variables |
| 110 | + |
| 111 | +Create a `.env` file (see `.env.example`): |
| 112 | + |
| 113 | +```bash |
| 114 | +LITELLM_API_KEY=your-litellm-key # copied to OPENAI_API_KEY automatically |
| 115 | +# OPENAI_BASE_URL= # optional: point at a different provider |
| 116 | +# SGP_API_KEY= # optional: tracing |
| 117 | +# SGP_ACCOUNT_ID= # optional: tracing |
| 118 | +# SGP_CLIENT_BASE_URL= # optional: tracing |
| 119 | +``` |
| 120 | + |
| 121 | +Happy building with Temporal + LangGraph! |
0 commit comments