showcase/shell-docs/src/content/reference/bot/functions/createBot.mdx
createBot is the entry point of @copilotkit/bot. It wires one or more platform adapters to an AG-UI agent and returns a Bot — the surface for registering turn handlers, interaction handlers, interrupt handlers, slash commands, and tools, plus start() / stop() lifecycle control.
For a complete walkthrough, see the Slack quickstart.
import { createBot } from "@copilotkit/bot";
function createBot<TStateSchema extends StandardSchemaV1 | undefined = undefined>(
opts: CreateBotOptions<TStateSchema>
): Bot<ThreadStateOf<TStateSchema>>;
createBot is generic over the per-thread state schema. Pass a Standard Schema as store.state and the returned Bot's handler callbacks receive a thread narrowed to StatefulThread<YourStateType> — thread.state() and thread.setState() are fully typed at the call site.
<PropertyReference name="adapter" type="StateStore" default="new MemoryStore()">
The pluggable persistence backend — the [StateStore](/reference/bot/types/StateStore) that backs action snapshots, per-thread state, transcripts, turn locks, and dedup. Defaults to an in-memory `MemoryStore` that is lost on restart. Pass a `createRedisStore(…)` or `createPostgresStore(…)` for durability across restarts and processes.
</PropertyReference>
<PropertyReference name="state" type="StandardSchemaV1">
A [Standard Schema](https://standardschema.dev/) (Zod, Valibot, ArkType, …) that describes the per-thread state shape. When set, `thread.setState()` validates at runtime and throws on a mismatch, and `thread.state()` is typed to the schema's output. The inferred type flows up to the `Bot`'s handler callbacks — you do not need explicit type annotations.
</PropertyReference>
<PropertyReference name="identity" type="(ctx: { adapter: string; author: PlatformUser; message: IncomingMessage }) => string | null | Promise<string | null>">
Resolve a stable, cross-platform identity key for the current user — typically an email address returned from a directory lookup. Return `null` to opt out for a given turn (transcripts are skipped). Must be configured together with `transcripts`; configuring one without the other throws at startup.
</PropertyReference>
<PropertyReference name="transcripts" type="TranscriptsConfig">
Cross-platform transcript storage. Must be configured together with `identity`.
<PropertyReference name="retention" type="string | number">
How long to keep transcript entries. Accepts a human-readable duration string (`"7d"`, `"2h30m"`) or milliseconds. Omit to keep entries indefinitely.
</PropertyReference>
<PropertyReference name="maxPerUser" type="number">
Maximum transcript entries to retain per user. When the list exceeds this length, the oldest entries are dropped. Omit for no cap.
</PropertyReference>
</PropertyReference>
<PropertyReference name="onLockConflict" type='"drop" | "force" | ((conversationKey: string, message: IncomingMessage) => "drop" | "force" | Promise<"drop" | "force">)' default='"drop"'>
What to do when a new turn arrives while a prior turn in the same conversation is still processing. `"drop"` silently discards the overlapping turn (default). `"force"` lets it proceed without waiting for the lock — both turns run concurrently. A function receives the conversation key and the incoming message and returns either decision.
</PropertyReference>
<PropertyReference name="lockTtl" type="number" default="60000">
TTL in milliseconds for the per-conversation turn lock. If a handler crashes without releasing the lock, it auto-expires after this window so subsequent turns are not permanently blocked.
</PropertyReference>
<PropertyReference name="dedupTtl" type="number" default="300000">
TTL in milliseconds for the inbound-event dedup window. Events with the same `eventId` delivered within this window are deduplicated and discarded; events outside the window are treated as new.
</PropertyReference>
import { createBot } from "@copilotkit/bot";
import { slack, defaultSlackTools, defaultSlackContext } from "@copilotkit/bot-slack";
const bot = createBot({
adapters: [
slack({
botToken: process.env.SLACK_BOT_TOKEN!,
appToken: process.env.SLACK_APP_TOKEN!,
}),
],
agent: (threadId) => makeAgent(threadId),
tools: [...defaultSlackTools, ...appTools],
context: [...defaultSlackContext, ...appContext],
});
bot.onMention(async ({ thread }) => {
await thread.runAgent();
});
await bot.start();
import { createBot } from "@copilotkit/bot";
import { createRedisStore } from "@copilotkit/bot-store-redis";
import { z } from "zod";
const WorkflowState = z.object({
step: z.enum(["idle", "awaiting-approval", "done"]),
lastUpdated: z.number(),
});
const bot = createBot({
adapters: [slack({ botToken, appToken })],
agent: (threadId) => makeAgent(threadId),
store: {
adapter: createRedisStore({ url: process.env.REDIS_URL }),
state: WorkflowState,
},
});
// `thread` is narrowed to StatefulThread<{ step: ...; lastUpdated: number }>
bot.onMention(async ({ thread }) => {
const current = await thread.state();
await thread.setState({ step: "awaiting-approval", lastUpdated: Date.now() });
await thread.runAgent();
});
const bot = createBot({
adapters: [slackAdapter, discordAdapter],
agent: (threadId) => makeAgent(threadId),
store: {
adapter: createRedisStore({ url: process.env.REDIS_URL }),
identity: async ({ author }) => lookupEmailForUser(author.id),
transcripts: { retention: "30d", maxPerUser: 500 },
},
});
bot.onMention(async ({ thread }) => {
// Injects prior cross-platform history, appends the user turn,
// runs the agent, and captures the assistant reply — all in one call.
await thread.runAgent({ transcript: true });
});
For a detailed guide on persistence and transcripts, see Persistence and Transcripts.
onMention handler is registered, all turns route to the mention handlers; otherwise onMessage handlers fire. Registering identical handlers on both never double-fires.ActionExpiredError internally; createBot swallows it, so the click is acked but ignored and no message is posted.runAgent — omitting agent is fine for bots that only post UI, but thread.runAgent() will throw.