How integrated from custom lang graph work flow wi...
# ask-ai
w
How integrated from custom lang graph work flow with agent
t
Thanks for reaching out, ajay. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — you’re asking how to integrate a custom LangGraph workflow with a LiveKit agent. I can help — quick clarification first so I give the right level of detail: • Do you want a short code example (Python) showing how to wrap a compiled LangGraph workflow and run it as the agent’s LLM, or do you need guidance on runtime config, callbacks, or debugging? • Which runtime are you using (Python environment, versions, and whether you’ve installed the LiveKit LangChain plugin)? If you want the quick path, I can show a concise Python snippet that: 1. installs the plugin, 2. compiles a LangGraph workflow, 3. wraps it with
langchain.LLMAdapter(graph=...)
, and 4. passes it into
AgentSession
so the workflow runs as the agent’s LLM. Tell me which option you prefer (example code vs runtime/config/troubleshooting) and your environment details and I’ll respond with the exact snippet and next steps. Sources: LangChain integration guide | LiveKit docs
w
Example code
t
Thanks — below is a concise Python example that shows how to compile a LangGraph workflow, wrap it with the LiveKit LangChain
LLMAdapter
, and pass it into an
AgentSession
so the workflow acts as the agent’s LLM. 1) Install the plugin:
Copy code
uv add "livekit-agents[langchain]~=1.3"
2) Example code:
Copy code
# example_agent_with_langgraph.py
from langgraph.graph import StateGraph
from livekit.agents import AgentSession, Agent
from livekit.plugins import langchain

# 1. Define and compile a simple LangGraph workflow
def create_workflow():
    graph = StateGraph()

    # Example nodes (replace with real nodes/logic)
    # Add states/nodes according to your LangGraph design.
    # This is illustrative; use actual node constructors you need.
    start = graph.add_state("start")
    ask = graph.add_state("ask_user")
    respond = graph.add_state("respond")

    graph.add_edge(start, ask)
    graph.add_edge(ask, respond)

    # Compile the graph to get the runtime PregelProtocol
    return graph.compile()

# 2. Wrap the compiled workflow with LLMAdapter
llm_adapter = langchain.LLMAdapter(
    graph=create_workflow(),
    # config optional, e.g. runtime settings or callbacks
    # config=my_runnable_config
)

# 3. Create an Agent (optional: configure callbacks, tools, etc.)
agent = Agent(name="langgraph-agent")

# 4. Start an AgentSession using the LLMAdapter as the session's llm
session = AgentSession(
    llm=llm_adapter,
    agent=agent,
    # Optionally configure STT, TTS, VAD, audio, and other runtime pieces here.
    # stt=..., tts=..., vad=...
)

# 5. Run the session (API depends on your environment; example placeholder)
# session.run() or await session.start() depending on sync/async runtime
Notes: • Replace the placeholder LangGraph nodes/edges with your actual states and node implementations. •
LLMAdapter
accepts
graph
(compiled workflow) and optional
config
for runtime settings or callbacks. • Configure
AgentSession
parameters (STT, TTS, VAD, async vs sync) to match your application runtime. If you want, I can: • Provide a complete runnable example with concrete LangGraph node types, • Show async usage (await/async session start), • Or add an example
RunnableConfig
for callbacks — which would you prefer?
w
Can also ingrate lang smith
t
Yes — you can integrate LangSmith (LangChain tracing) with a LiveKit agent that uses a LangGraph workflow by passing LangChain callbacks (e.g., LangChainTracer) through the `LLMAdapter`'s
config
parameter. Below is a concise Python example showing: • compiled LangGraph workflow wrapped by
LLMAdapter
• LangSmith tracing via
LangChainTracer
added to
config
• required environment variables note
Copy code
# example_langsmith_integration.py
from langgraph.graph import StateGraph
from livekit.agents import AgentSession, Agent
from livekit.plugins import langchain

from langchain.callbacks.tracers import LangChainTracer
from langchain.callbacks.manager import CallbackManager

def create_workflow():
    graph = StateGraph()
    # add your states/nodes/edges here
    return graph.compile()

# Create LangChain tracer (LangSmith)
tracer = LangChainTracer()  # optional: tracer(project_name="my-project")

# Pass callbacks via the Runnable config used by LLMAdapter
runnable_config = {
    "callbacks": [tracer]
}

llm_adapter = langchain.LLMAdapter(
    graph=create_workflow(),
    config=runnable_config
)

agent = Agent(name="langgraph-langsmith-agent")

session = AgentSession(
    llm=llm_adapter,
    agent=agent,
    # stt=..., tts=..., vad=...
)

# Start/run session according to your runtime (sync or async)
# e.g. await session.start()
Environment setup: • Set LangSmith / LangChain tracing env vars, e.g.
LANGCHAIN_TRACING_V2=true
and your LangSmith API key (e.g.,
LANGCHAIN_TRACING_V2_IN_MEMORY=false
and provider-specific key) so tracing uploads succeed. If you’d like, I can: • provide a fully runnable async example with concrete LangGraph nodes, • show tracer configuration options (project name, session metadata), or • help troubleshoot tracer auth/env var settings — which would you prefer? Sources: LangChain integration guide | LiveKit docs