> ## Documentation Index
> Fetch the complete documentation index at: https://apie.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# LangChain and LangGraph

> Instrument LangChain agents and LangGraph nodes with Ratri callback handlers.

You run LangChain agents or LangGraph workflows. You want tool calls and graph node executions tracked inside Ratri runs without wrapping every tool manually.

## Callback handler (recommended)

Pass `RatriCallbackHandler` to your agent's callbacks inside a run:

<CodeGroup>
  ```ts TypeScript theme={null}
  import { RatriCallbackHandler } from "@ratri-sh/sdk/integrations";
  import ratri from "./ratri.config";

  const handler = new RatriCallbackHandler(ratri, {
    defaultEnvironment: "production",
  });

  await ratri.withRun({ inputSummary: "LangChain agent run" }, async () => {
    const result = await agent.invoke(
      { input: "Summarize the incident" },
      { callbacks: [handler] },
    );
  });
  ```

  ```python Python theme={null}
  from ratri import RatriCallbackHandler
  from ratri.config import ratri

  handler = RatriCallbackHandler(ratri, default_environment="production")

  def run_agent(run):
      return agent.invoke(
          {"input": "Summarize the incident"},
          config={"callbacks": [handler]},
      )

  ratri.with_run({"inputSummary": "LangChain agent run"}, run_agent)
  ```
</CodeGroup>

The handler tracks tool start/end events and infers action/resource metadata from tool names.

## LangGraph node wrapper

Wrap individual graph nodes for step-level telemetry:

<CodeGroup>
  ```ts TypeScript theme={null}
  import { withLangGraphNode } from "@ratri-sh/sdk/integrations";

  await withLangGraphNode(
    ratri,
    {
      runId: run.id,
      nodeName: "triage",
      stepKey: "triage-node",
      stepIndex: 1,
    },
    async () => triageNode(state),
  );
  ```

  ```python Python theme={null}
  from ratri import with_langgraph_node

  with_langgraph_node(
      ratri,
      {
          "runId": run.id,
          "nodeName": "triage",
          "stepKey": "triage-node",
          "stepIndex": 1,
      },
      lambda: triage_node(state),
  )
  ```
</CodeGroup>

## LangChain tool step

For individual tool steps with explicit metadata:

<CodeGroup>
  ```ts TypeScript theme={null}
  import { withLangChainToolStep } from "@ratri-sh/sdk/integrations";

  await withLangChainToolStep(
    ratri,
    {
      runId: run.id,
      toolName: "search",
      actionType: "read",
      resourceType: "knowledge_base",
    },
    async () => searchTool.invoke("query"),
  );
  ```

  ```python Python theme={null}
  from ratri import with_langchain_tool_step

  with_langchain_tool_step(
      ratri,
      {
          "runId": run.id,
          "toolName": "search",
          "actionType": "read",
          "resourceType": "knowledge_base",
      },
      lambda: search_tool.invoke("query"),
  )
  ```
</CodeGroup>

### What you'll see

Tool calls and workflow steps in the run timeline. In monitor mode, guard evaluations appear for tools with inferred or explicit metadata.

## Example

See the LangChain example in the SDK repos:

* [javascript-sdk/examples/langchain-instrumented-agent.ts](https://github.com/ratri-sh/javascript-sdk/blob/main/examples/langchain-instrumented-agent.ts)
* [python-sdk/examples/langchain\_instrumented\_agent.py](https://github.com/ratri-sh/python-sdk/blob/main/examples/langchain_instrumented_agent.py)

## Next steps

<CardGroup cols={2}>
  <Card title="Choose how to instrument" icon="route" href="/getting-started/choose-how-to-instrument">
    Framework plugin tier overview.
  </Card>

  <Card title="Monitor mode" icon="eye" href="/guardrails/monitor-mode">
    Observe guard evaluations first.
  </Card>
</CardGroup>
