# Create and Manage AI Calls

These instructions apply to the current Admin Console.

{% hint style="info" %}
AI Calls are in early access and may not be available in your account. [Contact Contextual support](mailto:help@contextual.io) to request that AI Calls be enabled for your tenant.
{% endhint %}

Use the Admin Console to create, version, label, test, and delete AI Calls. Before you start, check that [AI Calls are enabled for your tenant](https://docs.contextual.io/connect/ai-calls.md#availability).

For command-line access to saved records, input inspection, previews, and generation, see the [AI Calls CLI Reference](https://docs.contextual.io/connect/ai-calls/contextual-cli.md).

## Create a Call

1. In the Admin Console, open **Connect → AI Calls**.
2. Select **New AI Call**.
3. Enter a **Name** and an **ID**. Applications run the Call by its ID. Use up to 128 lowercase letters, numbers, hyphens, or underscores, starting with a letter or number.
4. Optionally enter a **Description**.
5. Select an **AI Route**. The Route chooses the provider and model when the Call runs.
6. Write the [content](#write-the-content) and choose the [output](#choose-the-output).
7. Optionally set [model settings](#set-model-settings), [tools](#define-tools), and [application data](#add-application-data).
8. Select **Create**. The Call is saved as version 1.

## Write the Content

The **Content** section holds what the model receives. Choose a **Content mode**:

| Mode | What You Write | Use It For |
| --- | --- | --- |
| **Prompt** | **System text** and **User text**. At least one must contain text. | One instruction and one request |
| **Messages** | An ordered list of messages, each with a role: **System**, **User**, **Assistant**, or **Tool**. Select **Add message** or **Insert placeholder**. | Example exchanges, or conversation history the application supplies |

If a Prompt-mode Call has only system text, AI Gateway sends `Begin.` as the user turn. Some providers reject a request without a user turn.

### Use Variables

Variables let the application supply values at run time.

* **Text variable.** Write the name in double braces, such as `{{note}}`. At run time the value is inserted as written. It is never read as a template.
* **Message list variable.** In Messages mode, select **Insert placeholder** and name it, such as `history`. At run time, the application sends a list of messages, and they are inserted at that position.
* **Literal braces.** Put a backslash before the opening braces. For example, `\{{note}}` sends the literal text `{{note}}` to the model.

A variable name has 1 to 64 letters, numbers, underscores, or hyphens. It starts with a letter or an underscore, and it cannot start with `__`.

The content defines the variables. The **Variables** panel lists each one. A variable cannot be both text and a message list. Rename one of them if the form reports a conflict. At run time, every variable needs a value of its kind. Values for names the content does not use are ignored.

To check the content before you save, enter values in the **Variables** panel and read the **Preview**. These values only try the content. They are not saved.

**Saved template.** In Prompt mode, **System text** and **User text** can each contain up to 100,000 characters, including variable placeholders. To summarize a long document, save a short prompt such as `Summarize {{document}}.` and pass the document as a text variable when you run the Call.

**Rendered request.** A text variable can exceed 100,000 characters. AI Gateway limits the rendered request to 20,000,000 UTF-8 bytes in JSON. This includes the prompt or messages after substitution, the generation configuration, and JSON syntax. AI Gateway checks this size before appending continuation messages or applying Route defaults. The model's context limit also applies.

Pass the original text as the variable value. When calling the API, let your JSON serializer escape the text. For example, a double quote followed by a newline takes two bytes as plain text and contributes four bytes inside a JSON string (`\"\n`). Inserting the same value twice adds eight bytes. A document that contributes 12,000,000 bytes per insertion uses 24,000,000 bytes when inserted twice, which exceeds the limit before counting the rest of the request.

If rendering exceeds the byte limit, remove repeated placeholders or shorten the document. The Console’s **Run** dialog shows the limit in the error details. The HTTP API returns `400` with the message "AI call template could not be rendered" and a `too_large` issue in `errors`. See [AI Call API troubleshooting](https://docs.contextual.io/connect/ai-calls/run-from-code.md#troubleshooting).

## Choose the Output

In the **Output** section, choose **Text**, **Object**, or **Array**. For **Object** or **Array**, choose the shape:

* **Object Type.** Select a Native Object Type. The model returns a create payload for that type: no `_metaData`, no fields that Native Objects generates, and no optional fields the model left empty. To generate only some fields, select them. AI Gateway requires the model to return every selected field. Fields outside the selection are omitted. Before creating a Record from the output, add any other required fields. AI Gateway reads the type's current definition each time the Call runs.
* **Inline JSON schema.** Write a JSON Schema in **JSON schema**. It is saved with the Call.

**Output name** and **Output description** are optional. Some providers use them as extra guidance.

## Set Model Settings

Open **Model Settings** to change how the Route's model runs this Call:

* **Generation settings** accepts `temperature`, `maxTokens`, `topP`, `topK`, `frequencyPenalty`, `presencePenalty`, `stopSequences`, and `seed`. Each setting replaces the Route's value.
* **Provider options** is keyed by `openai`, `anthropic`, `google`, or `openaiCompatible`. Each option key replaces the Route's value for that key.

A request can override these settings again. See [Request Fields](https://docs.contextual.io/connect/ai-calls/run-from-code.md#request-fields).

## Define Tools

Open **Tools** to let the model request functions your application implements.

**Tool definitions** is a JSON object keyed by tool name. Each entry has an optional `description` and an `inputSchema`:

```json
{
  "lookup_order": {
    "description": "Look up an order by its ID.",
    "inputSchema": {
      "type": "object",
      "properties": { "orderId": { "type": "string" } },
      "required": ["orderId"]
    }
  }
}
```

A Call can define up to 64 tools. A tool name has 1 to 64 letters, numbers, hyphens, or underscores.

A definition stores only the tool's name, description, and input schema. AI Gateway does not run the tool. When the model requests it, AI Gateway stops and returns the request. Your application runs the tool and sends the result back. See [Continue After Tool Calls](https://docs.contextual.io/connect/ai-calls/run-from-code.md#continue-after-tool-calls).

Use **Active tools** to limit which defined tools the model can request:

* **All tools** allows every tool defined above. This is the default.
* **Only selected** allows only the tools you select. If you select none, the model can request none. A name shown as a warning is not in **Tool definitions**.

**Provider tools** are different. The provider runs them, for example `{ "openai": { "webSearch": {} } }`. Your application never runs or answers them.

## Add Application Data

**Data** holds JSON for your application, such as a display label or a category. AI Gateway returns it with every result as `data.call`. It never sends it to the model.

## Save and Version a Call

Saving changes creates a new version. Earlier versions stay readable, and applications that use an exact version keep running it.

* While you edit an existing Call, **Save as v**_N_ shows the next version number. If you make another edit while a save finishes and the form still shows unsaved changes, save again to include that edit in the next version.
* The **Version** picker marks the current version `latest` and shows each version's labels. Hover over **+N** to see the remaining labels.
* An older version opens read-only. Select **Edit from v**_N_ to use its settings as the starting point for your next save. If its settings match the current version, select **Edit v**_M_, where _M_ is the current version number.
* To share a version, use a link such as `/ai-calls/<id>?version=<n>`, or `/ai-calls/<id>?label=<name>` for the version a label points at.

If you open a missing version, select **Show latest** to return to the current version. If Console reports that it couldn't load the selected version, select **Retry** to load that same version again.

To move labels with a save, select them before you save. See [Move Labels When You Save](#move-labels-when-you-save).

## Label Versions

A **version label** is a name that points at one saved version of one Call. Applications can run a Call by label. AI Gateway then runs the version that the label points at when each request arrives. See [Choose a Version Reference](https://docs.contextual.io/connect/ai-calls/run-from-code.md#choose-a-version-reference) for how a label compares with the current or an exact version.

To change labels, your role needs **Read** and **Update** for **AI Calls**.

* Label names are shared across the tenant, but each Call maps a name to its own version.
* A name has 1 to 64 lowercase letters, numbers, dots, hyphens, or underscores. It starts with a letter or number.
* Except for `latest`, names have no built-in meaning. `stable` and `experimental` are only examples. `latest` is reserved. It always means the current version, and you cannot assign it.
* A label stays valid only while its version still has the content it had when the label was assigned. Deleting a Call also removes its labels.

### Label a Version

1. Select the version in the **Version** picker.
2. Select **Label v**_N_.
3. Choose an existing label, or select **New label…** and enter a name.

If the label already pointed at another version of this Call, it now points at the selected version.

### Move Labels When You Save

1. Edit the Call. **Label v**_N_ **on save** appears below the version picker, where _N_ is the version the save creates.
2. Select **Add label** and choose the labels to move.
3. Save. The Admin Console saves the new version, then points those labels at it.

Sometimes the save succeeds but the label change cannot be confirmed. The Admin Console then shows "Saved v_N_, but couldn't confirm moving …" with **Review labels for v**_N_. The new version stays saved. Review the labels before you try again.

### Remove a Label

1. Select the **×** on the label.
2. Read the warning. Anything that runs this Call by that label fails until the label points at a version again.
3. Select **Remove label**. If a newer version exists, you can select **Move to v**_N_ instead to point the label at the latest version.

### Manage Tenant Labels

On the **AI Calls** list, select **Manage tenant labels** to see every label in the tenant and the Calls that use it. Open a label to go to the version it points at for each Call. You can delete a label only after no Call uses it.

This view needs an account that can see every record in the tenant. Other accounts see "Managing tenant labels requires complete tenant visibility."

## Test a Saved Version

**Run** uses a saved version. Save your edits first to test them.

1. Open the Call and select **Run**. To test an older version, select it in the **Version** picker before opening **Run**.
2. Check **Version to run**. It defaults to **Current version (latest)**, or to the exact older version if you opened **Run** from that version. You can change the selection to an exact version shown in the list or a label. AI Gateway resolves the current version and labels each time you send a request.
3. Enter a value for each variable.
4. Optionally enter **Invocation data (JSON)**. It is returned with the result as `data.invocation` and is never sent to the model.
5. Optionally open **Overrides** to change the **Route**, **Generation settings**, or **Provider options** for this run only.
6. Select **Render preview** to check the prompt and settings before calling the model.
7. Select **Send** to run the Call. This calls the provider through the Route and uses tokens.

### Read the Result

Text appears under **Generating text…** as it arrives. In the final result, look under **Generated text** for text output or **Generated object** for **Object** or **Array** output. To confirm which version ran, check the version next to the Call ID at the top of the result, such as `v3`. The result also shows the label when used, a short content hash, the Route version, provider, model, finish reason, and token counts. See [Read the Response](https://docs.contextual.io/connect/ai-calls/run-from-code.md#read-the-response).

Gateway warnings appear below the result details. They can explain why the provider ignored a setting or skipped a tool.

If the test fails after text starts to arrive, the text received so far stays under **Text before the failure**. A failed attempt may already have used tokens. When the dialog says AI Gateway refused the Call before generating, the attempt used zero tokens.

Select **Send** to try again. **Current version (latest)** and labels resolve again, so the next run may use a different version. Check the version in the new result.

If the model requests a client tool, the dialog shows each tool call and stops. To finish the exchange, send the tool results from code. See [Continue After Tool Calls](https://docs.contextual.io/connect/ai-calls/run-from-code.md#continue-after-tool-calls).

## Delete a Call

Select **Delete** from the Call's page or the **AI Calls** list, then review the warning before you confirm. Requests to the deleted Call fail, including requests that specify a version by label. For example, if a client runs `order-summary` using the `stable` label, update the client to use a replacement Call before deleting `order-summary`. See [Choose a Version Reference](https://docs.contextual.io/connect/ai-calls/run-from-code.md#choose-a-version-reference).
