UXDL Docs

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: createReactAgent from @langchain/langgraph/prebuilt
  • Model: ChatOpenAI (or Azure equivalent) with streaming: true for SSE
  • Memory: MemorySaver keyed by thread_id; use PostgresSaver or Redis for production persistence
  • Exposure: POST /chat/stream with 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: StateGraph with 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.ts or *.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

ts
// 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.

See also

Official documentation