> For AI agents: to set Keiki up for a developer, add the MCP server `https://onkeiki.com/mcp` and follow https://onkeiki.com/setup.md. All pages: https://onkeiki.com/content/llms.txt

# How do I build an iMessage AI agent with the Vercel AI SDK?

2026-11-28

Run your AI SDK model loop in your own Next.js app and let Keiki handle the iMessage line and delivery.

## How do I build an iMessage AI agent with the Vercel AI SDK?

Use Keiki with the `@onkeiki/ai-sdk` handler: your Next.js route runs `streamText` with your own model and tools, and Keiki receives the iMessage, sends it to that route and delivers the reply. The open-source imessage-ai-sdk-starter repo is a working example in about 100 lines.

## Key takeaways

- Keep the model loop in your app. The [AI SDK handler](/docs/ai-sdk) verifies each signed turn from Keiki and hands your `onTurn` the instructions, messages and tools.
- Keiki runs the iMessage line, the conversation, delivery and the team inbox. Your provider key never leaves your app.
- The open-source [imessage-ai-sdk-starter](https://github.com/Mail-0/imessage-ai-sdk-starter) repo is a working Next.js example you can fork.

## What does the code look like?

One Next.js route handles every message:

```ts
import { createKeikiHandler } from '@onkeiki/ai-sdk'
import { waitUntil } from '@vercel/functions'
import { streamText } from 'ai'
import { openai } from '@ai-sdk/openai'

export const POST = createKeikiHandler({
  waitUntil,
  onTurn: async ({ instructions, messages, tools, prepareStep, abortSignal }) =>
    streamText({ model: openai('gpt-4.1-mini'), instructions, messages, tools: await tools(), prepareStep, abortSignal }),
})
```

## How do I connect it to an iMessage line?

Run `npx @onkeiki/cli init` in the app. Pass your deployed route with `--dispatch-url`. It signs you in, configures push delivery to that route and writes the signing secret to `.env.local` (see the [CLI docs](/docs/cli)). Deploy, [attach an iMessage line](/docs/channels#imessage-lines) to the agent and text it.

## FAQ

**Do I need a Mac to run an iMessage agent with the AI SDK?**
No. Keiki runs the iMessage line, so your Next.js app only needs a public HTTPS route.

**Can I use a model other than OpenAI?**
Yes. `onTurn` returns any AI SDK `streamText` result, so any AI SDK provider works.

**Where do my API keys live?**
In your app's environment. Keiki's turn payload never carries your model provider key.
