Skip to content

How To Compile a Graph

Devrajsinh Gohil edited this page Aug 30, 2026 · 1 revision

How-To: Compile a StateGraph

This guide covers advanced compilation options with agentmesh_adapter.compile().


Basic Compilation

import agentmesh_adapter
from langgraph.graph import StateGraph

builder = StateGraph(MyState)
# ... configure nodes and edges ...

app = agentmesh_adapter.compile(builder)

Compilation with Checkpointer (State Persistence)

Attach a checkpointer to enable session resumption, time-travel, and state inspection:

from langgraph.checkpoint.memory import MemorySaver
import agentmesh_adapter

memory = MemorySaver()
app = agentmesh_adapter.compile(builder, checkpointer=memory)

# Invoke with thread configuration
config = {"configurable": {"thread_id": "session-101"}}
result = app.invoke({"messages": ["Start session"]}, config=config)

# Retrieve saved state
saved_state = app.get_state(config)
print("Saved state:", saved_state)

What Happens During Compilation?

When agentmesh_adapter.compile(builder) is called:

  1. Schema Introspection: Scans builder.state_schema and builder.channels for typing.Annotated channel reducers.
  2. Node Registration: Registers Python callables, unwrapping LangGraph Runnable wrappers.
  3. Static Edge Resolution: Maps __start__, standard directed edges, and __end__ sinks into the C++ FastNode table.
  4. Conditional Edge Binding: Binds router functions to source nodes for dynamic branch evaluation.
  5. Engine Instantiation: Returns an AgentMeshCompiledApp wrapper around the native agentmesh_core.PyWorkflowEngine.\n

Clone this wiki locally