Add an AI chat to your app

Your Cloud app can have a chat that its users talk to: a support desk, an assistant, a booking chat. The chat answers with an AI model through Playcode AI Gateway, and each message uses your workspace's credits.

The chat does these by default:

  • Each user has their own conversations, kept in your app's database. An answer goes on when the page reloads or the app restarts.
  • Actions wait for approval. An action the user confirms for themselves - a booking, an email in their name - shows them Approve and Deny in the chat. An action only you allow - a refund, a discount - waits for you on an admin page of your app.
  • Each user has a daily limit. Past the credits you set for one day, the chat says so before it calls a model.
  • Long conversations fit. Old tool results are cleared first, then a short summary stands for the older part.
On this page

Ask the agent

Add a support chat to my app - each customer has their own conversation, refunds wait for my approval, and a customer can spend at most 1 credit a day.

It builds the chat page and its backend, tests it with one real message, which uses your credits, and tells you what a message costs.

Do it yourself

#
  1. Check that the app has AI: see Add AI to your app.
  2. Open the chat that a new Cloud app has: the /chat page, with its backend at /api/ai/chat/. Its agent is in backend-nest/src/ai/examples/booking-agent/. Change the agent's instructions and tools for your app. Each part in code: Build a chat with the SDK.
  3. Mark each tool that the user confirms with approval: true. The chat then shows the user Approve and Deny before the tool runs.
  4. For an action only you allow, such as a refund, do not use approval: true: the person in the chat would approve it. Let the tool save a request, and approve requests on an admin page of your app. The Leads page of a new Cloud app is an example of an admin page.
  5. Set the daily limit per user in the chat's controller: limits: { user: { creditsPerDay: 1 } }.
  6. Send one message on the page, and approve one action, in the chat or on your admin page. In Settings, Usage, the project's AI Gateway row counts the requests.

What each user costs

#

Ask the agent what your app's AI cost per user, per model or per day. It reads your app's usage from Playcode. Your app tells Playcode which of its users each request is for; the requests without a user are counted together.

If it doesn't work

#

The chat says the daily limit is reached

#

The user spent the credits of their day. The limit opens again at 00:00 UTC. Raise creditsPerDay if it is too low for your users.

An action waits and nothing happens

#

An action that the user confirms waits for that user: in their conversation, they choose Approve or Deny. A request that waits for you is on your admin page.

Sending a message answers 409

#

The conversation has an answer in progress, or an action that waits for approval. Wait for the answer, or approve, deny or stop it first.

Limits

#
  • The chat needs a Cloud app. A static site has no server for its conversations.
  • A user's conversations are theirs: another user cannot open them.
  • The daily limit is counted per user. Two answers of one user at the same moment can pass it by one model call each.