# LangChain (/docs/integrations/langchain)



LangChain and LangGraph work with TokenPolice after one install step. Init as usual — see
[init parameters](/docs/sdk/init) — there is nothing LangChain-specific to call. The provider
calls underneath (OpenAI, Anthropic, Gemini, …) are metered through LangChain, and tool calls get
their own steps in the trace automatically.

## Python [#python]

Install the `langchain` extra:

```bash
pip install "token-police[langchain]"
```

Then `tp.init(...)` **before** you import the module that imports LangChain:

```python
import token_police as tp

tp.init(api_key=..., base_url=..., firewall="enforce")

import agent   # imports LangChain — must come after init
```

## Node [#node]

Add the **companion package** alongside the SDK. There is no API to call and nothing to import
from it — the base SDK detects it at startup and enables LangChain instrumentation:

```bash
npm i token-police token-police-langchain
```

On native ESM you also need two **static** imports before `init()`, so the SDK can patch the ESM
copies of `@langchain/core` that LangGraph actually calls:

```typescript
import * as tp from "token-police";
import * as lcChatModels from "@langchain/core/language_models/chat_models";
import * as lcCallbacks from "@langchain/core/callbacks/manager";

tp.init({
  /* … see init parameters … */
  instrumentModules: {
    langChain: { chatModelsModule: lcChatModels, callbackManagerModule: lcCallbacks },
  },
});

// Static ESM imports all run before this file's code, so keep every other
// @langchain/* import out of the entry module and load it after init instead.
const { runTurn } = await import("./agent.js");
```

<Callout type="info">
  The companion is a separate package so apps that don't use LangChain don't pay for its
  dependency tree. Install it only if you use LangChain — and never import from it directly.
</Callout>

<Callout type="warn" title="Reroute does not switch the model on LangChain">
  A Reroute rule does not switch the model on LangChain or LangGraph — those calls still run on
  the original model. Metering and blocking work normally. See [Actions](/docs/rules/actions#reroute).
</Callout>

Supported: `@langchain/core` 0.3.x and 1.x (tested with `langchain` 0.3 and `@langchain/langgraph` 1.x).
On `@langchain/core` 0.3, pnpm reports an unmet peer from the companion's bundled instrumentor
(it wants `>=1.0.0`); this is expected and safe — the SDK wires LangChain's callback manager itself.

<Callout type="warn">
  On native ESM, import order still matters for the provider clients LangChain calls under the hood.
  See [Node & ESM](/docs/sdk/node-esm).
</Callout>

## Next [#next]

<Cards>
  <Card title="Node & ESM" href="/docs/sdk/node-esm" description="Import order and instrumentModules in full." />

  <Card title="Support matrix" href="/docs/integrations/matrix" description="Every provider and framework at a glance." />
</Cards>
