Skip to Content
TutorialsIntegration PlaybooksChocolate FactoryOverview

Chocolate Factory Integration

Add AI agents to your app

What is Chocolate Factory?

Chocolate Factory is an AI agent platform. You build and configure an agent — its system prompt, model, tools, and guardrails — in the Chocolate Factory Portal, then call it from your app to power chat, generate content, or return structured data.

Perfect for:

  • Customer-facing chat / support widgets
  • AI-generated dashboard summaries and reports
  • Structured data extraction (e.g. feeding a chart)
  • Form autofill, email drafts, generated documents
  • Anything where you want an LLM behind a feature without building the LLM plumbing yourself

This connector only works from your app’s Next.js code — API routes and Client Components. Its values reach your app’s environment only, not the data service’s.

What You’ll Need

Chocolate Factory, connected

Auto-provisioned automatically, or bring your own key — see Step 1.

An agent

Created by you in the Chocolate Factory Portal. Buildpad can connect your project to Chocolate Factory, but it can’t create or pick an agent for you.

The agent’s ID

Copied from the Portal. You’ll hand this to your coding assistant every time a feature needs it.

A good initial prompt

A concise prompt goes a long way in minimizing iterations to implement the feature.

Step 1: Connect Chocolate Factory

Connect the project

  1. Open your project → Connectors in the sidebar (or check the Chocolate Factory card during project creation).
  2. Click Connect.
  3. Choose a mode:
    • Auto-provision (default) — Buildpad creates a Chocolate Factory project for you at {your org name}/{your project name}/BuildPad and stores the resulting key. When it is done, the card shows Connected, or Error if it failed.
    • Bring your own key — paste an existing Chocolate Factory project-scoped API key (starts with cf_). Buildpad also accepts a key that starts with agk_. It refuses a key with any other prefix with the message Chocolate Factory keys start with "cf_" or "agk_". Use this if you already manage a Chocolate Factory project outside Buildpad.
  4. For Auto-provision, click Auto-provision now. For Bring your own key, click Connect.

This stores two things for your project: CHOCOLATE_FACTORY_API_KEY and CHOCOLATE_FACTORY_BASE_URL. The base URL isn’t a field you fill in when connecting — Buildpad sets it to the main Chocolate Factory platform automatically, and the connect form can’t be tricked into overriding it. It’s still just an env var in your own app once pushed, though — if you ever need to point at a different Chocolate Factory instance, you (or your coding assistant) can change CHOCOLATE_FACTORY_BASE_URL like any other env var.

Create your agent

  1. Open the Chocolate Factory Portal. On the connector card, click the globe icon (tooltip Open Chocolate Factory).
  2. Create — or find — the agent for the feature you’re building. See Agent Configuration for what to set: model, system prompt, and tools.
  3. Copy its Agent ID.

Agent IDs aren’t stored by Buildpad. There’s no list-agents step your coding assistant can do for you — you paste the ID into your prompt every time a feature needs one.

Step 2: Tell the Agent What You Want

Once Chocolate Factory is connected, your coding assistant already knows how to reach it — you don’t need to paste API keys or base URLs into a prompt, just the Agent ID and what you want built.

Example 1: Customer support chat widget

Add the Buildpad AI chat to this app as a floating widget. Agent ID: a1b2c3d4-5e6f-47a8-xxxx-xxxxxxxxxxxx (this is my customer support agent) I want the widget to: - Appear on every page, bottom-right - Match my app's brand color - Start with the message "Hi! How can I help you today?" Please set this up.

Example 2: AI dashboard summary

Add an AI-generated sales summary card to my dashboard. Agent ID: c3d4e5f6-7a8b-49c0-xxxx-xxxxxxxxxxxx (configured to return JSON: { labels: string[], values: number[] }) The card should: - Fetch the last 30 days of orders from my "orders" collection - Pass that data to the agent - Render the result as a bar chart Please build this.

Example 3: Custom chat page

Build a separate chat page at /support with my own design. Agent ID: e5f6a7b8-9c0d-4e1f-xxxx-xxxxxxxxxxxx I want: - My own message bubbles and page layout - A "thinking…" indicator while the agent is responding Please use the Buildpad AI chat module's AiChat component inside my page layout.

For a chat, your assistant uses Buildpad’s AI Chat module  through its add-ai-chat skill: AiChatWidget for Example 1, AiChat inside your own layout for Example 3, or AiChatPanel for a panel docked beside the page with an Ask AI button. It doesn’t write a chat component of its own. If your project has no add-ai-chat skill (a starter downloaded before the module shipped), add the latest Buildpad skills first with npx skills add buildpad-ai/skills. In the Chocolate Factory Portal, turn the chat agent’s Memory off: everyone who uses your app reaches it with the same key.

Chat and other streaming responses call Chocolate Factory directly from the browser, not through your own app. Amplify Hosting runs your Next.js API routes as Lambda functions with a hard ~30 second response limit, and buffered invocation on top of that — a streamed reply gets silently swallowed inside the Lambda and the connection dies before it reaches the browser. Proxying works fine locally (pnpm dev) but fails in production for anything that streams. See Streaming Chat and Amplify’s Timeout below for what this means for your API key.

One-off, non-streaming calls (a dashboard summary, a structured-JSON extraction, form autofill) aren’t affected — those resolve well under 30 seconds, so your assistant still proxies them through your own app route by default, keeping the key server-side.

Step 3: Test the Connection

Start the app

Run pnpm dev and open the page where the feature was added.

Confirm it appears

The widget (or your custom UI) should render — no console errors about a missing agent ID or API key.

Send a test message

Type something and submit. You should get a response.

Check the network tab

For a chat widget or other streaming feature, the request should hit Chocolate Factory’s URL directly (e.g. .../api/agents/<agentId>/run) — that’s expected, not a bug; see Streaming Chat and Amplify’s Timeout. For a one-off, non-streaming call (a dashboard summary, structured output), it should hit your own app’s route instead, keeping the key server-side.

Confirm the reply streams or resolves

Chat responses should stream in; one-off calls (e.g. a dashboard summary) should resolve with the expected shape.

Repeat after deploying

Env vars connected here don’t automatically reach your deployed app — ask your assistant to push them (see Troubleshooting) and re-test on the live URL.

Streaming Chat and Amplify’s Timeout

Amplify Hosting runs your app’s Next.js API routes as Lambda functions it manages internally. Two things about that matter for chat:

  1. A hard ~30 second response limit. Amplify’s SSR layer cuts a slow request off with a clean 504 around 28s; the Lambda’s own hard timeout fires at 30s with a raw 500 if it gets that far.
  2. Buffered invocation. Unless a Lambda is explicitly configured for streaming invoke mode — not something Amplify Hosting exposes — nothing is sent to the caller until the function returns. A ReadableStream response from your own API route doesn’t actually stream to the browser in production; it silently accumulates inside the Lambda and hits the same ~30s wall, just failing differently (a raw 500 instead of a graceful 504).

This only shows up after deploying — next dev streams fine, since Node’s dev server writes chunks straight to the socket with no Lambda in between.

Why the fix is calling Chocolate Factory directly from the browser: if the request never touches your app’s own API routes, no Amplify SSR Lambda is in the path, so its 30s limit and buffering don’t apply — real Server-Sent Events reach the browser as they’re generated. By default, this is what your assistant builds for a chat feature: the AI Chat module’s browser-direct backend (buildpad add ai-chat-routes), which calls the run endpoint from the browser and streams the answer. It adds no Chocolate Factory SDK. To keep the key away from anyone who isn’t signed in, the assistant makes the module’s config route (/api/ai-chat/config) answer only signed-in users.

The tradeoff: this means the project key CHOCOLATE_FACTORY_API_KEY (cf_…, which reaches every agent in the project) is exposed to any signed-in user who loads the page. That’s accepted as the default for chat because the alternative — proxying — doesn’t stream on Amplify. Leave CHOCOLATE_FACTORY_AGENT_KEY unset for chat: Chocolate Factory refuses an agent key sent from a browser, so every chat call would fail. To keep the key on the server, ask your assistant for the module’s server proxy (ai-chat-proxy-routes). It streams only on a long-lived Node server; on Amplify, each answer arrives all at once.

If your feature doesn’t stream (a dashboard summary, structured JSON, form autofill), none of this applies — those calls resolve well under 30s and stay proxied through your own server by default, with the key kept out of the browser.

Common Questions

Do I need a separate agent for every feature?

Not necessarily — one agent can back multiple features if its system prompt is general enough. Many projects do split by feature (a support agent, a summary agent, etc.) since each has a different system prompt and output shape. If you use more than one, ask your assistant to name each env var with a label, e.g. CHOCOLATE_FACTORY_AGENT_ID_DASHBOARD_SUMMARY.

Can end users see my Chocolate Factory API key?

For non-streaming calls (dashboard summaries, structured output), no — those are proxied through your own server, so the real key never reaches the browser.

For chat and other streaming features, yes — the key is exposed to the browser by design, because Amplify’s ~30 second Lambda limit and buffered invocation break streamed responses if they’re proxied through your own app. See Streaming Chat and Amplify’s Timeout.

Widget or custom chat UI — which should I use?

Both use Buildpad’s AI Chat module , copied into your app’s components/ui/ai-chat/. It calls the Chocolate Factory run endpoint (/api/agents/<agentId>/run) directly from the browser, with no Chocolate Factory SDK. Use Example 1 for a chat on every page: AiChatWidget floats in a corner, and AiChatPanel docks beside the page behind an Ask AI button. Use Example 3 when you want to set the full design of a separate chat page: it puts the module’s AiChat inside your own layout.

Can the agent do more than chat?

Yes. Any agent can be called as a plain function — text or data in, text out — which works for dashboard cards, generated documents, form autofill, or structured JSON for a chart. The agent’s system prompt (configured in the Portal) decides the output shape.

Something Not Working?

Can’t Connect

Tell the agent:

Getting a 401 calling Chocolate Factory. Can you check: 1. CHOCOLATE_FACTORY_API_KEY and CHOCOLATE_FACTORY_BASE_URL are set in .env.local 2. The connector shows "Connected" on the project's Connectors page 3. Whether I need to restart `pnpm dev` after adding the env vars

Widget Doesn’t Appear

Tell the agent:

Added the Chocolate Factory widget but nothing shows up on the page. Agent ID: [paste it] Can you check the browser console for errors and confirm the widget component is actually rendered in the layout?

With the AI Chat module, the chat shows nothing until its config route answers. Open /api/ai-chat/config while signed in: a 503 means CHOCOLATE_FACTORY_BASE_URL, CHOCOLATE_FACTORY_API_KEY or CHOCOLATE_FACTORY_AGENT_ID is missing. Next.js reads .env.local only when it starts, so restart pnpm dev after you add one.

In the browser’s network tab, look at the request to /run. A CORS error usually means CHOCOLATE_FACTORY_AGENT_KEY is set: remove it. A 404 means the Agent ID is wrong, and a 400 can mean the agent is not active.

Works Locally, Not After Deploying

Tell the agent:

The Chocolate Factory feature works with `pnpm dev` but not on the deployed app. Can you check whether CHOCOLATE_FACTORY_API_KEY, CHOCOLATE_FACTORY_BASE_URL, and CHOCOLATE_FACTORY_AGENT_ID are set in the Amplify environment, and redeploy if you add or change any of them?

If it’s a chat or streaming feature specifically, and it fails only after deploying with a 500 or 504, that’s likely Amplify’s ~30s Lambda timeout rather than missing env vars — tell the agent:

My streaming Chocolate Factory feature times out or 500s on Amplify but works fine in pnpm dev. Can you check whether this is calling Chocolate Factory through my own API route (which won't stream reliably on Amplify) instead of directly from the browser?

Agent Replies, But I Don’t See Tool Calls

The AI Chat module does not show tool calls: while the agent uses its tools, the chat shows one working word until the answer arrives. To see which tools ran, with their input and output, open the conversation in Chocolate Factory’s Monitoring. See Observability.

Real Examples

Example: Sales lead follow-up drafts

Add a "Draft Follow-up" button on each row of my leads table. Agent ID: a1b2c3d4-5e6f-47a8-xxxx-xxxxxxxxxxxx (configured to write a short, friendly follow-up email) When clicked: - Send the lead's name, company, and last contact date to the agent - Show the drafted email in a modal - Let me copy it or edit before sending Please build this.

Example: Support ticket summarizer

When a support ticket has more than 5 messages, show an AI summary at the top. Agent ID: c3d4e5f6-7a8b-49c0-xxxx-xxxxxxxxxxxx - Pass the full message thread to the agent - Show the summary in a collapsible card above the thread - Add a "Regenerate" button Please set this up.

Advanced Features

Once the basics work:

Multiple Agents

I want to add a second Chocolate Factory agent for a different feature. Agent ID: a7b8c9d0-1e2f-4a3b-xxxx-xxxxxxxxxxxx Purpose: summarizing weekly analytics Please add this as its own labeled environment variable, separate from my existing agent, and use it for a new /analytics/summary route.

Structured Output

I want this agent to return JSON instead of chat text, so I can render it as a chart. Agent ID: c9d0e1f2-3a4b-4c5d-xxxx-xxxxxxxxxxxx Expected shape: { labels: string[], values: number[] } Please parse the response and handle the case where the agent returns something that isn't valid JSON.

Quick Checklist

Before going live:

  • Connector shows Connected on the Connectors page
  • Agent ID is set as an env var, not hardcoded in a prompt or component
  • Feature works with pnpm dev locally
  • Env vars pushed to the Amplify deployment and redeployed
  • Tested on the live URL, not just locally
  • Network tab shows the expected pattern for your feature — direct-to-Chocolate-Factory for chat/streaming, your own app route for one-off calls
  • Checked what happens when the agent errors or times out

Remember

Credentials stay server-side for one-off calls, not for chat — non-streaming features (summaries, structured output) keep your API key server-side by default; chat and other streaming features call Chocolate Factory directly from the browser instead, because Amplify’s Lambda timeout breaks proxied streaming. See Streaming Chat and Amplify’s Timeout.

Agent IDs are yours to manage — Buildpad never stores or picks them for you.

One playbook, many UIs — chat panels and pages, dashboards, summaries, and forms all use the same connected agent.

Deploying is a separate step — connecting Chocolate Factory doesn’t push env vars to your live app automatically.

Great for embedding AI without building your own LLM plumbing — the agent’s behavior lives in the Chocolate Factory Portal, not your codebase.

Stuck? Tell your coding assistant: “I need help with the Chocolate Factory integration” and describe the issue.

Last updated on