{"slug": "how-to-build-an-ai-agent-for-slack-with-chat-sdk-and-ai-sdk", "title": "How to Build an AI Agent for Slack with Chat SDK and AI SDK", "summary": "Vercel has released a guide for developers to build AI-powered Slack agents using Chat SDK and AI SDK, enabling autonomous responses to mentions, conversation history tracking, and tool calling without managing infrastructure. The integration combines Chat SDK's platform handling with AI SDK's ToolLoopAgent for reasoning loops, while Vercel AI Gateway and Redis provide production-ready state management and streaming responses. Developers can wire up multi-turn conversations and scale tool sets using the provided framework, which supports Node.js 18+, Slack workspace installation, and Redis instances.", "body_md": "You can build an AI-powered Slack agent that responds to mentions, maintains conversation history, and calls tools autonomously using Chat SDK and AI SDK. Chat SDK handles the platform integration (webhooks, message formatting, thread tracking), while AI SDK's `ToolLoopAgent`\n\nmanages the reasoning loop that lets your agent call tools and act on results. Together with Vercel AI Gateway and Redis for state, you get a production-ready Slack agent without managing infrastructure or juggling provider SDKs.\n\nThis guide will walk you through building a Slack agent with Chat SDK, AI SDK's `ToolLoopAgent`\n\n, and Claude via the [Vercel AI Gateway](https://vercel.com/ai-gateway). You'll wire up streaming responses, tool calling, and multi-turn conversation history, then scale your tool set for production with toolpick.\n\nBefore you begin, make sure you have:\n\n- Node.js 18+\n[pnpm](https://pnpm.io/)(or npm/yarn)- A Slack workspace where you can install apps\n- A Redis instance (local or hosted, such as\n[Upstash](https://vercel.com/marketplace/upstash)) - A\n[Vercel account](https://vercel.com/signup)with an AI Gateway API key\n\nChat SDK is a unified TypeScript SDK for building chatbots across Slack, Teams, Discord, and other platforms. You register event handlers (like `onNewMention`\n\nand `onSubscribedMessage`\n\n), and the SDK routes incoming webhooks to them. The Slack adapter handles webhook verification, message parsing, and the Slack API. The Redis state adapter tracks which threads your bot has subscribed to and manages distributed locking for concurrent message handling.\n\nAI SDK's `ToolLoopAgent`\n\nwraps a language model with tools and runs an autonomous loop: the model generates text or calls a tool, the SDK executes the tool, feeds the result back, and repeats until the model finishes. When you pass a model string like `\"anthropic/claude-sonnet-4.6\"`\n\n, and host your application on Vercel, the AI SDK will route the request through the AI Gateway automatically.\n\nChat SDK accepts any `AsyncIterable<string>`\n\nas a message, so you can pass the agent's `fullStream`\n\ndirectly to `thread.post()`\n\nfor real-time streaming in Slack.\n\nCreate a new Next.js app and add the Chat SDK, AI SDK, and adapter packages:\n\nThe `chat`\n\npackage is the Chat SDK core. The `@chat-adapter/slack`\n\nand `@chat-adapter/state-redis`\n\npackages are the [Slack platform adapter](https://chat-sdk.dev/adapters/slack) and [Redis state adapter.](https://chat-sdk.dev/adapters/redis) The `ai`\n\npackage is the AI SDK, which includes the AI Gateway provider and `ToolLoopAgent`\n\n. `zod`\n\nis used to define tool input schemas.\n\nThe [Vercel Plugin](https://vercel.com/docs/agent-resources/vercel-plugin) equips your AI coding agent (e.g., Claude Code) with skills, specialist agents, slash commands, and more.\n\nGo to [api.slack.com/apps](https://api.slack.com/apps), click Create New App, then From a manifest.\n\nSelect your workspace and paste this manifest:\n\nAfter creating the app:\n\n- Go to Install App, and install the app to your workspace\n- Go to OAuth & Permissions > OAuth Tokens and copy the Bot User OAuth Token\n- Go to Basic Information > App Credentials and copy the Signing Secret\n\nYou'll replace the `request_url`\n\nplaceholders with your real domain after deploying (or a tunnel URL for local testing).\n\nCreate a `.env.local`\n\nfile in your project root:\n\nThe Slack adapter reads `SLACK_BOT_TOKEN`\n\nand `SLACK_SIGNING_SECRET`\n\nautomatically. The Redis state adapter reads `REDIS_URL`\n\n. AI SDK uses `AI_GATEWAY_API_KEY`\n\nto authenticate with the Vercel AI Gateway, or alternatively, use [OIDC authentication](https://ai-sdk.dev/providers/ai-sdk-providers/ai-gateway#oidc-authentication-vercel-deployments).\n\nYou can create an AI Gateway API key from your [Vercel dashboard](https://vercel.com) under AI Gateway and click Create an API Key.\n\nCreate `lib/tools.ts`\n\nwith the tools your agent can call. This example defines a weather tool and docs tool, but you can add any tools your use case requires:\n\nEach tool has a `description`\n\n(which tells the model when to use it), an `inputSchema`\n\n(a Zod schema that the model fills in), and an `execute`\n\nfunction that runs when the tool is called.\n\nCreate `lib/bot.ts`\n\nwith a `ToolLoopAgent`\n\nand a `Chat`\n\ninstance:\n\nWhen someone @mentions the bot, `onNewMention`\n\nfires. The handler subscribes to the thread (to track future messages in that thread) and streams the agent's response. For follow-up messages, `onSubscribedMessage`\n\nretrieves the full thread history using `thread.allMessages`\n\n, converts it to the AI SDK message format with `toAiMessages`\n\nand passes it to the agent so it has a complete conversation context.\n\nThe `fullStream`\n\nis preferred over `textStream`\n\nbecause it preserves paragraph breaks between tool-calling steps. Chat SDK auto-detects the stream type and handles Slack's native streaming API for real-time updates.\n\nCreate the API route at `app/api/webhooks/[platform]/route.ts`\n\n:\n\nThis creates a `POST /api/webhooks/slack`\n\nendpoint. The `waitUntil`\n\noption ensures your event handlers finish processing after the HTTP response is sent, which is required on serverless platforms where the function would otherwise terminate early.\n\n- Start the dev server:\n- Expose it with a tunnel:\n- Copy the tunnel URL (for example,\n`https://abc123.ngrok-free.dev`\n\n) and update both Event Subscriptions and Interactivity Request URLs in your[Slack app settings](https://api.slack.com/apps)to`https://abc123.ngrok-free.dev/api/webhooks/slack`\n\n- Invite the bot to a channel (\n`/invite @AI Agent`\n\n) - @mention the bot with a question. You should see a streaming response appear in the thread. Try asking it to use one of your tools, such as \"What's the weather in San Francisco?\"\n\nFirst, link your project and add your environment variables:\n\nAlternatively, add them in the Vercel dashboard under Settings > Environment Variables.\n\nThen deploy:\n\nUpdate the Event Subscriptions and Interactivity Request URLs in your Slack app settings to your production URL, for example `https://my-slack-agent.vercel.app/api/webhooks/slack`\n\n.\n\nWhen deployed to Vercel, AI Gateway supports OIDC-based authentication, so you can also authenticate without a static API key. See the [AI Gateway authentication docs](https://vercel.com/docs/ai-gateway/authentication-and-byok#oidc-tokens).\n\nCheck that your Slack app has the `app_mentions:read`\n\nscope and that the Event Subscriptions Request URL is correct. Slack sends a challenge request when you first set the URL, so your server must be running.\n\nChat SDK uses Slack's native streaming API for smooth updates. If you're seeing issues, check that your Redis connection is stable, as the SDK uses distributed locks to manage concurrent messages.\n\nIf the agent calls a tool but no result appears, check for errors in your tool's `execute`\n\nfunction. AI SDK surfaces tool execution errors back to the model, which may attempt to recover. Add error handling in your tools and check your server logs for details.\n\nFor long-running threads, the conversation history can exceed the model's context window. Consider limiting the number of messages you pass to the agent by slicing the history array or by using a summarization step for older messages.\n\nThe agent in this guide has two tools. In production, a Slack agent often grows to 15, 20, or 30 tools as you integrate services like GitHub, [Linear](https://vercel.com/marketplace/linear), [Upstash](https://vercel.com/marketplace/upstash), calendars, and deploy pipelines. At that scale, every tool definition is sent to the model on every step, which increases token costs and makes it harder for the model to pick the right tool.\n\n[toolpick](https://www.npmjs.com/package/toolpick) solves this by indexing your tools at startup and selecting only the most relevant ones for each step. It hooks into `ToolLoopAgent`\n\nvia the `prepareStep`\n\noption, so you don't need to change your handler logic.\n\nBuild an index from your full tool set. toolpick uses a combination of keyword matching and semantic embeddings to find the best tools for each step:\n\nFor higher accuracy with vague queries (like \"ship it\" or \"ping the team\"), add a re-ranker model that uses a cheap LLM to pick the final candidates:\n\nPass `toolIndex.prepareStep()`\n\nto your `ToolLoopAgent`\n\n. This sets `activeTools`\n\non each step, so the model only sees the tools it needs, while all tools remain available for execution:\n\nIf the model can't find a relevant tool in the current selection, toolpick automatically moves to the next page of results. After two misses, it exposes all tools as a fallback. Your agent never gets stuck in a loop, unable to find the right tool.\n\nFor an extra accuracy boost, enable `enrichDescriptions`\n\nto expand your tool descriptions with synonyms and alternative phrasings. This runs a one-time LLM call during `warmUp()`\n\nat server startup. You can also persist the computed embeddings to disk with `fileCache`\n\nso subsequent restarts skip the embedding API call entirely:\n\nThis setup is optional for agents with a handful of tools, but becomes worthwhile as your tool set grows. The per-step cost of re-ranking with `gpt-4o-mini`\n\nis approximately $0.0001, which is negligible compared to the token savings from sending fewer tool definitions to the primary model.\n\nChat SDK supports multiple platforms from a single codebase. The event handlers and agent logic you've already defined work identically across all of them, since the SDK normalizes messages, threads, and reactions into a consistent format.\n\nTo add Microsoft Teams or another platform, register an additional adapter:\n\nThe existing webhook route in `src/index.ts`\n\nalready uses a `:platform`\n\nparameter, so Teams webhooks would be handled at `/api/webhooks/teams`\n\nwith no additional routing code.\n\nStreaming behavior varies by platform. Slack uses its native streaming API for smooth real-time updates, while Teams, Discord, and Google Chat fall back to a post-then-edit pattern that throttles updates to avoid rate limits. You can adjust the update interval with the `streamingUpdateIntervalMs`\n\noption when creating your `Chat`\n\ninstance.\n\nSee the [Chat SDK adapter directory](https://chat-sdk.dev/adapters) for the full list of supported platforms.", "url": "https://wpnews.pro/news/how-to-build-an-ai-agent-for-slack-with-chat-sdk-and-ai-sdk", "canonical_source": "https://vercel.com/kb/guide/how-to-build-an-ai-agent-for-slack-with-chat-sdk-and-ai-sdk", "published_at": "2026-06-06 04:34:58+00:00", "updated_at": "2026-06-06 04:46:54.677951+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "ai-infrastructure", "large-language-models", "generative-ai"], "entities": ["Chat SDK", "AI SDK", "ToolLoopAgent", "Vercel AI Gateway", "Redis", "Slack", "Claude", "Upstash"], "alternates": {"html": "https://wpnews.pro/news/how-to-build-an-ai-agent-for-slack-with-chat-sdk-and-ai-sdk", "markdown": "https://wpnews.pro/news/how-to-build-an-ai-agent-for-slack-with-chat-sdk-and-ai-sdk.md", "text": "https://wpnews.pro/news/how-to-build-an-ai-agent-for-slack-with-chat-sdk-and-ai-sdk.txt", "jsonld": "https://wpnews.pro/news/how-to-build-an-ai-agent-for-slack-with-chat-sdk-and-ai-sdk.jsonld"}}