Use your own AI key

Use a key from OpenAI, Google, Anthropic or xAI to pay that provider directly for AI usage. This option is called BYOK (bring your own key).

Agents use Platica's account by default. When you connect your key, that provider's models use your account. You can also choose models that require your own key.

Before you start

You need:

  • Your Platica API key in PLATIC_API_KEY.
  • A key from one of the four providers.
  • An agent with its id in AGENT_ID.

To use the platform, see Models and your own keys.

  1. See the providers

    List all four providers, their states (status and health) and their models. They appear even if you haven't connected a key. (List providers)

    import { Platic } from "platic-sdk";
    
    const platic = new Platic({ apiKey: process.env.PLATIC_API_KEY! });
    
    const providers = await platic.aiProviders.list();
    for (const provider of providers) console.log(provider.provider, provider.status, provider.health);

    status is not_connected (it uses Platica's account), verifying, connected, invalid or disabled. In each model, ownKeyOnly: true means it only works with your own key.

  2. Connect your key

    Connect the key to store and test it. If the provider already had a key, this call replaces it. You don't need to send the key in later reads. (Connect a key)

    const openai = await platic.aiProviders.connect("openai", {
      value: process.env.OPENAI_API_KEY!,
      fallback: "platform",
    });
    
    if (openai.status !== "connected") {
      console.log(openai.health, openai.lastError);
    }

    provider is openai, google, anthropic or xai, and the key can be up to 4,096 characters. The key is stored encrypted as a workspace secret and no read returns it: only status, health, fallback, secretName and models.

    When you connect the key, Platica generates a short response with each model. The test uses a few tokens and can take up to 200 seconds. The limit is 10 tests per hour per provider.

    If the key is invalid, the call returns 200 with status: "invalid" and the reason in lastError.

  3. Choose the fallback

    If you top up your balance, activate a model or the key becomes invalid, you can test the stored key again without sending it. (Test a key)

    const checked = await platic.aiProviders.verify("openai");
    console.log(checked.status, checked.health, checked.lastError);

    health can be ok, key_rejected, no_credit or no_model_access. With no_credit, Platica retries every 10 minutes and resumes using the key when you top up.

    The fallback defines what to do if your key fails:

    • platform, the default, uses Platica's account and incurs AI charges. Models that require your own key stop responding.
    • none stops responses from that provider's models.

    You can change the fallback without replacing the key. (Change the fallback)

    await platic.aiProviders.update("openai", { fallback: "none" });
  4. Choose the agent's model

    Change model in the agent's draft and publish the version.

    To add fallback models, set fallbackModels with provider/model values. If you add a fallback the agent didn't have, you need at least one own key with status: "connected". Without it, you get 422 with MODEL_FALLBACKS_REQUIRE_OWN_KEY. (Update the draft, Agent configuration)

    const agentId = process.env.AGENT_ID!;
    
    await platic.agents.draft.update(agentId, { model: "claude-sonnet-5-5-medium" });
    await platic.agents.publish(agentId);

    The model can carry the reasoning level as a suffix (claude-sonnet-5-5-medium). A level the model doesn't accept answers 422 with UNSUPPORTED_REASONING_EFFORT, and an unknown model, INVALID_CONFIG. This example assumes you connected an Anthropic key; use a model of the provider you connected.

Check it worked

Use List providers to check that your provider has status: "connected" and health: "ok", and the models you need are available. Send a message with Talk to your agent and check usage in your provider account.

To receive provider health changes, use the model_provider.status.updated webhook. See Receive webhooks.

To remove the key, disconnect it. Before, check which agents or models depend on it: they lose that workspace key.

Next steps