---
docId: doc_8641e0337c1f43d594d78c428a80fdf5
title: Use your own AI key
description: "Connect your OpenAI, Google, Anthropic or xAI account and choose what happens if your key fails."
---

<script>
  import { Note, Steps, Ways } from '$lib/components';
</script>

# Use your own AI key

<span id="what-youll-build"></span>

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](/guides/create-an-agent) with its id in `AGENT_ID`.

To use the platform, see [Models and your own keys](/help/settings/models).

<Steps>
<Steps.Step>

**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](/api-reference/ai-providers/list))

<Ways tabs="api,sdk,asistente">
<Ways.Way name="api">

```bash
curl https://api.platic.ai/v1/ai-providers \
  -H "Authorization: Bearer $PLATIC_API_KEY"
```

</Ways.Way>
<Ways.Way name="sdk">

```ts
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);
```

</Ways.Way>
<Ways.Way name="asistente">
<Ways.Say>

Which AI providers do I have connected with my own key?

</Ways.Say>

Tool: [`list_ai_providers`](/mcp/tools#list_ai_providers).

</Ways.Way>
</Ways>

`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.

</Steps.Step>
<Steps.Step>

**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](/api-reference/ai-providers/connect-key))

<Ways tabs="api,sdk,asistente">
<Ways.Way name="api">

```bash
curl -X PUT https://api.platic.ai/v1/ai-providers/openai \
  -H "Authorization: Bearer $PLATIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "value": "'$OPENAI_API_KEY'", "fallback": "platform" }'
```

</Ways.Way>
<Ways.Way name="sdk">

```ts
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);
}
```

</Ways.Way>
<Ways.Way name="asistente">
<Ways.Say>

I want to use my own OpenAI key.

</Ways.Say>

Tool: [`connect_ai_provider`](/mcp/tools#connect_ai_provider). To keep the key out of the chat, connect it yourself in **Settings → Models** and ask your assistant to check it.

</Ways.Way>
</Ways>

`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](/api-reference/secrets) 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`.

</Steps.Step>
<Steps.Step>

**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](/api-reference/ai-providers/test-key))

<Ways tabs="api,sdk,asistente">
<Ways.Way name="api">

```bash
curl -X POST https://api.platic.ai/v1/ai-providers/openai/verify \
  -H "Authorization: Bearer $PLATIC_API_KEY"
```

</Ways.Way>
<Ways.Way name="sdk">

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

</Ways.Way>
<Ways.Way name="asistente">
<Ways.Say>

Test my OpenAI key again.

</Ways.Say>

Tool: [`verify_ai_provider`](/mcp/tools#verify_ai_provider).

</Ways.Way>
</Ways>

`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](/api-reference/ai-providers/set-fallback))

<Ways tabs="api,sdk,asistente">
<Ways.Way name="api">

```bash
curl -X PATCH https://api.platic.ai/v1/ai-providers/openai \
  -H "Authorization: Bearer $PLATIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "fallback": "none" }'
```

</Ways.Way>
<Ways.Way name="sdk">

```ts
await platic.aiProviders.update("openai", { fallback: "none" });
```

</Ways.Way>
<Ways.Way name="asistente">
<Ways.Say>

If my OpenAI key fails, don't fall back to Platica's account.

</Ways.Say>

Tool: [`update_ai_provider`](/mcp/tools#update_ai_provider).

</Ways.Way>
</Ways>

</Steps.Step>
<Steps.Step>

**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](/api-reference/agents/drafts/update), [Agent configuration](/api-reference/agents/configuration))

<Ways tabs="api,sdk,asistente">
<Ways.Way name="api">

```bash
curl -X PATCH https://api.platic.ai/v1/agents/$AGENT_ID/draft \
  -H "Authorization: Bearer $PLATIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "model": "claude-sonnet-5-5-medium" }'

curl -X POST https://api.platic.ai/v1/agents/$AGENT_ID/publication \
  -H "Authorization: Bearer $PLATIC_API_KEY"
```

</Ways.Way>
<Ways.Way name="sdk">

```ts
const agentId = process.env.AGENT_ID!;

await platic.agents.draft.update(agentId, { model: "claude-sonnet-5-5-medium" });
await platic.agents.publish(agentId);
```

</Ways.Way>
<Ways.Way name="asistente">
<Ways.Say>

Make the Sales agent use Claude Sonnet 5.5 with medium reasoning, and publish it.

</Ways.Say>

Tool: [`update_agent_draft`](/mcp/tools#update_agent_draft) and [`publish_agent_version`](/mcp/tools#publish_agent_version).

</Ways.Way>
</Ways>

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.

</Steps.Step>
</Steps>

## Check it worked

Use [List providers](/api-reference/ai-providers/list) 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](/guides/chat-api) and check usage in your provider account.

To receive provider health changes, use the `model_provider.status.updated` webhook. See [Receive webhooks](/guides/webhooks).

To remove the key, [disconnect it](/api-reference/ai-providers/disconnect-key). Before, check which agents or models depend on it: they lose that workspace key.

<Note type="important">

Using your own key doesn't remove other Platica charges. Review [Usage](/help/settings/usage) and your provider's billing.

</Note>

## Next steps

- [Test it before you publish](/guides/evals): compare the draft with the published version after changing the model.
- [AI providers](/api-reference/ai-providers): states, errors and limits.
- [Agent configuration](/api-reference/agents/configuration): reasoning levels and fallback models.
