6 min

Put a Dewee agent on your website: a chatbox that thinks, uses tools, and never leaks your token

One script tag, one backend route, and your website has a real AI agent on duty: streamed reasoning, tool calls, agent switching, running on Next.js, Astro, React, Vue, PHP. A detailed guide + the open-source dewee-webchat repo 😁

Also in: Tiếng Việt · 中文 · 한국어 · 日本語

dewee-webchat page: Your Dewee agent, on any website
webchat.dewee.sh: the official dewee-webchat playground

One <script> tag, one backend route, and your website has a real agent on duty. Not an FAQ bot that answers "Sorry, I didn't understand your question".

This week I sat down to do something that sounds super "boring": putting a chatbox on a website.

If you build websites, you've probably dropped in a chat widget or two. Copy the script, paste it in the footer, done. Easy peasy.

But the moment you want to plug in an AI agent (one with tools, MCP, reasoning), things start to feel off:

  • Where does the gateway token live? Put it in client-side JavaScript and you're basically handing it out to the whole internet :))))
  • The agent spends 10–20 seconds running tools, and visitors stare at a frozen screen thinking the site is dead.
  • Every framework does it differently: Next.js one way, Astro another, Vue yet another, and the PHP folks… there are still a LOT of you out there.

So I just built dewee-webchat: an open-source package for putting a Dewee agent on any website.

The widget handles the display. Your backend holds the keys. The gateway brings the brains.

1/ Why not call the gateway straight from the browser?

The first question everyone asks: "The gateway already has a WebSocket, why not connect straight from the browser and keep it simple?"

Because to connect, you need a token.

And a token sitting in the browser means anyone who opens DevTools can grab it. Grab the token and they can call every agent you have, burn through your model budget, and read other people's sessions.

So dewee-webchat's architecture is split into 3 layers:

mermaidsequenceDiagram
    autonumber
    participant B as Browser (dewee-chat)
    participant S as Backend /api/dewee
    participant G as Dewee gateway
    B->>S: POST /session
    S-->>B: visitor token (HMAC-signed)
    B->>S: POST /chat (visitor token + message)
    S->>S: check origin, rate limit, agent allowlist
    S->>G: WebSocket (gateway token) chat.send
    G-->>S: thinking / tool.call / tool.result / chunk
    S-->>B: SSE stream, event by event
    G-->>S: run.completed
    S-->>B: done
The browser only holds a visitor token. The gateway token stays put on the backend.
  • The Browser only gets an HMAC-signed visitor token. That token only means something to your backend.
  • The Backend holds the gateway token, checks origin, rate limit, and the agent allowlist, and only then opens a WebSocket to the gateway.
  • The Gateway streams back: thinking, tool calls, tool results, and the answer chunk by chunk.

Each visitor is mapped to their own user on the gateway (webchat-<id>). So everyone only sees their own chat history; visitor A can't open the site and read visitor B's chats.

2/ When the agent thinks, let visitors… watch it think

This is my favorite part.

Good agents are usually slow. They have to read pages, call MCP, look up data. A question like "Read the pricing page for me and compare the 2 plans" can take 15 seconds.

15 seconds of staring at a spinning spinner is a long time.

So the widget streams the agent's whole working process:

  1. 💭 Thinking: what the agent is thinking (can be turned off).
  2. 🔧 Tool call: which tool the agent is calling, with which arguments.
  3. ✅ Tool result: what the tool returned (collapsed, click to expand).
  4. ✍️ The answer streams in word by word, with markdown, tables, code blocks.
dewee-webchat widget showing Thought for 9s, tool read url done, and a summary table
The agent thinks for 9 seconds, calls the read_url tool to read dewee.sh, then answers with a table. The visitor sees the whole process.

Visitors see the agent working instead of hanging. Totally different feeling.

3/ Add it to your website in 3 steps

Step 1: Create a narrowly scoped API key

Don't use an admin token for your website. Create a separate key that can only chat:

bashdewee api-keys create --name "website chat" --scopes operator.read,operator.write

If the key ever leaks, revoke just that key and nothing else is affected.

Step 2: Add the backend route

bashnpm i dewee-webchat

Set the environment variables:

bashDEWEE_GATEWAY_URL=https://gateway.your-domain.com
DEWEE_GATEWAY_TOKEN=...            # the key you just created, ONLY on the server
DEWEE_WEBCHAT_SECRET=...           # openssl rand -base64 32
DEWEE_AGENTS=support,sales         # which agents visitors may chat with

Then mount the handler. The handler is a standard (Request) => Response function, so it runs pretty much anywhere: Cloudflare Workers, Next.js, Astro, Bun, Deno, Node.

ts// Next.js: app/api/dewee/[...path]/route.ts
import { createDeweeHandler, optionsFromEnv } from "dewee-webchat/server";

export const dynamic = "force-dynamic";
const handler = createDeweeHandler(optionsFromEnv(process.env));
export { handler as GET, handler as POST, handler as OPTIONS };
Express, Cloudflare Workers, PHP? Click to see
js// Express
import { createDeweeHandler, optionsFromEnv, toNodeHandler } from "dewee-webchat/server";
app.all("/api/dewee/*path", toNodeHandler(createDeweeHandler(optionsFromEnv(process.env))));
ts// Cloudflare Workers
import { createDeweeHandler, optionsFromEnv } from "dewee-webchat/server";
let dewee;
export default {
  fetch(req, env) {
    if (new URL(req.url).pathname.startsWith("/api/dewee/"))
      return (dewee ??= createDeweeHandler(optionsFromEnv(env)))(req);
    return env.ASSETS.fetch(req);
  },
};

For PHP you only need one file, dewee-webchat.php. No Composer, just drop it on shared hosting and it runs:

php<?php
require __DIR__ . '/dewee-webchat.php';
DeweeWebchat::loadEnvFile(__DIR__ . '/.env');
DeweeWebchat::handle();

Step 3: Add the widget

Play with it right here, and the code for your framework is generated automatically 👇

dewee-webchat embed code generator
Tweak the options, pick your framework, copy it into your project.
Chạy trong sandbox cô lập; mạng chỉ qua proxy được duyệt.

Under the hood it's all one Web Component, <dewee-chat>, living in Shadow DOM, so your site's CSS can't break it, and it won't break your CSS either.

4/ Commonly used config options

OptionDefaultWhat it does
modefloatingfloating = bubble in the corner of the screen, inline = embedded right in the page
agentdefault agentPick the agent
allow-agent-switchtrueLet visitors switch between agents
show-reasoningtrueShow/hide the thinking
show-tool-callstrueShow/hide tool calls
themelightlight, dark, auto
accent-color#5446e8Accent color
persisttrueKeep chat history across page reloads
localefollows the pagevi, en

There's also a JavaScript API: open(), send(text), newConversation()… plus events like dewee:reply and dewee:error for firing analytics.

Little trick: want an "Ask the agent about this product" button on your product detail page? Just do:

jsdocument.querySelector("dewee-chat").send("Help me out with this product: " + product.name);

5/ Security: what's already built in

Put AI on a public website and you have to think about people trying to break it. The package already handles:

  • Đã xong: The gateway token never reaches the browser.
  • Đã xong: HMAC-signed visitor tokens, each visitor gets their own user on the gateway.
  • Đã xong: Agent allowlist: visitors can only chat with agents you allow, even if they tamper with the request.
  • Đã xong: Origin allowlist: other sites can't piggyback on your backend.
  • Đã xong: Rate limit: by default 20 messages/minute per visitor, 10 new sessions/10 minutes per IP.
  • Đã xong: Message length limit (4000 characters by default).
  • Đã xong: Cloudflare Turnstile support to block bots.

6/ Real running examples, not slide-deck demos

I deployed all the examples to Cloudflare, so you can click in and chat right away:

The playground has a form for tweaking the config live: change the color, toggle reasoning, switch agents, and it generates the matching code for each framework. Tweak, copy, paste, and it runs.

7/ A few things I learned building this

One: Web Components are the right choice for embeddable widgets.

Write it once; React/Vue/Astro are just thin wrappers. Shadow DOM takes care of the CSS-conflict nightmare for you.

Two: SSE is plenty, no need for WebSocket on the browser side.

Browser → backend is an HTTP POST that returns an SSE stream. It goes through every proxy, CDN, and serverless platform. WebSocket is only needed for the backend → gateway leg.

Three: Showing the agent's work is UX, not a side feature.

Visitors trust the answer more when they can see the agent actually read the pricing page instead of making things up.

Four: Don't forget the PHP crowd.

A huge number of SME websites in Vietnam still run on WordPress and shared hosting. A single dependency-free PHP file is the fastest way to get them an AI agent on their site.

Which stack will you put dewee-webchat on?


The package is MIT, so use it however you like. Try it on your site, and if you hit any bugs, open an issue on GitHub.

Don't have a Dewee agent yet? Head over to dewee.sh, spin one up, then come back here and put it on your site 😁