docs/agents/get-started/langchain.mdx
Connect a LangChain agent to Novu Connect. Return a LangChainAgentConfig from onMessage; Novu invokes the agent and delivers the reply on Slack. The same handler works on other providers after you link them.
This guide uses Slack first and assumes an existing Node.js app.
<Note> Starting from scratch? See [Scaffold with the CLI](#scaffold-with-the-cli). </Note>OPENAI_API_KEY or ANTHROPIC_API_KEY (or another LangChain provider key)In the Novu dashboard:
Alternatively run npx novu connect, choose LangChain and Slack, and skip creating a starter project when prompted. Screenshots: Create a Slack app.
</Step>
Add to .env.local:
NOVU_SECRET_KEY=your-novu-secret-key
OPENAI_API_KEY=sk-... # or ANTHROPIC_API_KEY
Create app/novu/agents/<your-identifier>.ts. Match agent('...') to the dashboard Identifier.
export const supportBot = agent('support-bot', { onMessage: async (_message, ctx) => ({ model: 'openai:gpt-4o', system: 'You are a helpful support agent. Keep answers short.', }), });
</Tab>
<Tab title="Claude">
```typescript
import { agent } from '@novu/framework/langchain';
export const supportBot = agent('support-bot', {
onMessage: async (_message, ctx) => ({
model: 'anthropic:claude-sonnet-4-20250514',
system: 'You are a helpful support agent. Keep answers short.',
}),
});
Export from app/novu/agents/index.ts:
export { supportBot } from './support-bot';
Returning { model, system } is a LangChainAgentConfig. Novu calls createAgent().invoke() and posts the final text. Novu maps ctx.history on this path.
On Next.js, add LangChain packages to serverExternalPackages for Turbopack. See Next.js and Turbopack.
</Step>
import { serve } from '@novu/framework/next';
import { supportBot } from '../../novu/agents';
export const { GET, POST, OPTIONS } = serve({
agents: [supportBot],
});
For Express, Hono, and other servers, see Connecting your app. </Step>
<Step> ## Run locally and message Slacknpx novu dev --port 4000
DM or @mention the bot in Slack. The reply should come from your LangChain handler in the same thread. </Step>
</Steps>Gate a tool with needsApproval:
import { tool } from '@langchain/core/tools';
import { agent } from '@novu/framework/langchain';
import { z } from 'zod';
const issueRefund = tool(
async ({ orderId }) => ({ orderId, status: 'refunded' }),
{
name: 'issueRefund',
description: 'Issue a refund for an order',
schema: z.object({ orderId: z.string() }),
},
);
export const supportBot = agent('support-bot', {
onMessage: async (_message, ctx) => ({
model: 'openai:gpt-4o',
system: 'You are a helpful support agent. Use issueRefund only when the user asks for a refund.',
tools: [issueRefund],
needsApproval: (toolCall) => toolCall.name === 'issueRefund',
}),
});
See Tool approval and the LangChain reference.
npx novu connect
Choose LangChain, authenticate the model, connect Slack, and accept the starter project. Then run npm run dev:novu.
For an existing codebase, use Connect an existing app instead.
<Prompt description="Connect my LangChain agent to Slack with Novu" icon="plug" actions={["copy", "cursor"]}>
Follow https://docs.novu.co/agents/get-started/langchain
Add Novu bridge + LangChain config handler to this repo and connect Slack. Prefer the existing project over scaffolding a new one.
ALWAYS:
agent('id') to the Novu dashboard agent IdentifierNEVER: