Add AI to your app

Your Cloud app can use AI for the people who use it: a button that drafts a product description, a chat that answers your customers (Add an AI chat to your app), a form that reads an invoice photo. The app calls the models through Playcode AI Gateway, so there is no AI account or key to buy, and each request uses your workspace's credits.

On this page

Ask the agent

Add a button to my product form that writes the description with AI.

It switches on AI for the app if needed, writes the backend code and tests it with one real request, which uses your credits.

Do it yourself

#
  1. Check that the app has AI. In the project, open Settings, choose Secrets and look for PLAYCODE_AI_URL, marked Managed. If it is missing, ask the agent to switch on AI for the app: it adds the setting and restarts the app.

  2. Call the model from the app's backend. A new Cloud app has an AI service in backend-nest/src/ai/: it is playcode.ai from the Playcode SDK. generate() answers, stream() answers while the model writes, and agent() lets the model call your app's own functions. They take files, images and PDFs the model reads with the prompt: see Read images and PDFs in your app.

    import { Injectable } from '@nestjs/common'
    import { AiService } from '../ai'
    
    @Injectable()
    export class DescriptionService {
      constructor(private readonly ai: AiService) {}
    
      async write(product: string): Promise<string> {
        const { text } = await this.ai.generate({
          model: WRITER_MODEL, // a model id the agent picked for the feature
          system: 'Write a two-sentence product description. Plain text only.',
          prompt: product,
        })
        return text
      }
    }
    
  3. Give the feature an endpoint of its own in the backend, with a sign-in check, and call that endpoint from the page. The browser never calls Playcode AI Gateway and never sees the key.

  4. Try the feature once. In Settings, Usage, the project's AI Gateway row counts the requests, with their calls and tokens.

Models

#

Your app names a model by its id. Ask the agent which model fits the feature: it reads each model's card from Playcode, with what the model reads (text, images, PDFs) and its limits, its price in credits, where its maker processes the call, and whether Playcode's own tests recommend it for reading documents. Your code can read the same cards: this.ai.models().

playcode.ai calls every model the same way. For a feature it does not cover, a model maker's own SDK works too, pointed at Playcode: this.ai.endpoint(api) gives its address and key, where api is the model card's interface - Claude models use the Anthropic SDK, GPT and most others the OpenAI SDK, and Gemini models Google's @google/genai.

import Anthropic from '@anthropic-ai/sdk'

const client = new Anthropic(this.ai.endpoint('anthropic')) // { baseURL, apiKey }

Where requests are processed

#

Each request goes from your app to Playcode AI Gateway, on Playcode's servers in Germany, and from there to the model's maker, which processes it. The model's card says where: EU means in the European Union - the Gemini models your app can call run on Google Cloud's EU multi-region, in EU member states only; global means any country where the maker runs, with no promise of one country or region; and unknown means the maker publishes no location. To keep requests in the EU, use a model whose card says EU.

When a model retires

#

Playcode never moves your app to another model, or to another price, on its own. Before a model retires, its card turns deprecated and names its successor (replacedBy) and the date (retiresAt). Until then, your app keeps working. Ask the agent to move the app to the successor: with the model id in one place in your code, that is a one-line change. Once the model retires, a request to its id is refused with "model '...' is not available".

Limit what an app's AI spends

#

Your building, your app's computers and your app's AI spend one balance: the workspace's credits. Nothing in it is kept aside for the app's AI, so when your building uses up the credits, the app's AI requests are refused at once. To keep them running, turn on auto-recharge with a monthly card maximum that covers both.

Every app has a daily AI limit: Playcode's default of 50 credits a day, until you set your own, higher or lower. To change it for one app, ask the agent, for example "Set a daily AI limit of 20 credits for this app." Past the limit, the app's AI requests are refused until 00:00 UTC, or until you raise the limit. A limit of 0 turns the app's AI off. When the limit stops the app's AI, you get one email about that app that day, with the limit and how to raise it.

To see the limits, open Settings, AI in your project. For your published app and for its development copy, it shows the daily limit and where it comes from, what the app's AI used today, and the monthly budget. When the limit stops the app's AI, it says so, with the time it stopped and when it starts again. You can change a limit there, go back to Playcode's default, or ask the agent.

To cap one app for a month, give it a monthly AI budget in credits: ask the agent, for example "Set a monthly AI budget of 50 credits for this app." Past the budget, the app's AI requests are refused until the 1st of the next month (UTC), or until you raise or clear the budget. An app has no budget until you set one.

A limit or a budget is a ceiling, not a reserve: it keeps no credits aside. Your published app and its development copy each have their own.

The monthly email line in Settings, Billing & plans, Spending controls sends an email only. It never stops your app's AI.

If it doesn't work

#

The request fails with 402 and insufficient_balance

#

The workspace is out of credits. Add credits; do not retry the request in a loop.

The request fails with 402 and budget_exceeded

#

One of this app's own limits is spent, and the message names it and says how much the app used. "This app's daily AI limit is reached": the app's AI works again after 00:00 UTC, and the agent can raise the limit. "This app's monthly AI budget is reached": ask the agent to raise or clear the budget, or wait for the 1st (UTC). Do not retry the request in a loop.

The answer stops early with playcode_balance_exhausted or playcode_app_budget_exhausted

#

The credits, or this app's daily AI limit or monthly AI budget, ran out while the model was answering. The text that arrived was delivered and billed.

The request fails with 413 and request_too_large

#

The request is over the gateway's limit of 32 MiB, and the error names the limit in bytes. Shrink photos, split long PDFs or send less text. See Read images and PDFs in your app.

The request fails with 400 and server_tool_not_supported

#

The request turned on a tool that the model's maker runs and bills itself, such as web search or code execution. Playcode AI Gateway does not pass these tools on. Write the tool as a function in your app, and give it to the model as an ordinary tool.

The request fails with 429 and rate_limited or daily_cap

#

A request limit was reached. The error body's playcode part says which limit refused and what remains. Wait, then try once more.

"model '...' is not available"

#

Playcode does not offer that model id through this SDK, the id is misspelled, or the model has retired. Ask the agent for the list. For a retired model, ask it to move the app to the successor the model's card named.

The request fails with 403 and missing_scope

#

The app's key cannot use AI yet. Ask the agent to switch on AI for the app: it renews the key and restarts the app.

The request fails with 401 and invalid_key

#

The app uses an old key. A PLAYCODE_SECRET_KEY line in the project's .env.local file can hide the current one: remove that line. See Secrets and environment.

The response has the header x-playcode-error-source: upstream

#

The model's own provider refused the request, for example for a wrong parameter or an input that is too long. Fix the request.

Limits

#
  • AI needs a Cloud app. A static site has no server to keep the key.
  • The key works only through Playcode AI Gateway. It is not a key for a model maker's own API.
  • Model ids pass through unchanged, with no aliases. A model that is not in Playcode's list is refused.
  • One request carries at most 32 MiB, its files included.
  • By default, an app can send 600 AI requests a minute. Past that, a request is refused with 429 and rate_limited, and the error says how long to wait (retryAfterMs).
  • Tools that the model's maker runs and bills itself, such as web search, are refused.
  • The models on offer and their prices change over time. Ask the agent before you choose.
  • Requests stop when the workspace's credits run out, at an app's daily AI limit (50 credits a day unless you set another), and at its monthly AI budget if you set one. See Credits and balance.