Agents & Pipelines
ReAct chat agents, StateGraph pipelines, and direct LLM calls.
ReAct chat agents, StateGraph pipelines, and direct LLM calls inside the API repo.
ReAct agent (multi-turn chat)
- Type:
createReactAgentfrom@langchain/langgraph/prebuilt - Model:
ChatOpenAI(or Azure equivalent) withstreaming: truefor SSE - Memory:
MemorySaverkeyed bythread_id; usePostgresSaveror Redis for production persistence - Exposure:
POST /chat/streamwith SSE events (token,done,error) - Durability: Persist completed turns to MongoDB (or Postgres) so history survives restarts
See Streaming & realtime for SSE event shapes.
StateGraph (structured pipelines)
- Type:
StateGraphwith explicit nodes and edges (linear or parallel fan-out) - Invoke:
graph.invoke(input)from a service function - Output: Typed result object; validate before
insertOne/updateOne - Regenerate: Re-invoke only when input fields that affect generation change
Direct LLM calls (no graph)
Use for single-shot structured outputs when a full agent is unnecessary.
Rules:
- Keep prompts in a dedicated module (
prompts.tsor*.prompt.ts). - Parse JSON defensively; normalize with a schema helper.
- Cache on the document. Do not re-call the LLM on every page view.
- Use the same env and tracing conventions as graph-based features.
For when to choose this vs a graph, see AI Overview.
Adding tools to a ReAct agent
// agents/chat/tools.ts
import { TavilySearchResults } from "@langchain/community/tools/tavily_search";
export const chatTools = [new TavilySearchResults({ maxResults: 5 })];The ReAct loop selects tools automatically. Document third-party env keys (e.g. TAVILY_API_KEY) in Integrations.