# AI requests

> Send prompts to OpenAI, Anthropic, Google Gemini and OpenAI-compatible models from Cloak Proxy, stream answers, give the model MCP tools and test the output.

Source: https://docs.subcloak.com/cloak-proxy/api-client/protocols/ai/

An AI request sends a prompt to a language model and shows its answer as it streams in. You pick the provider and model, write a system prompt and a user prompt with `{{variables}}`, and can give the model tools from your saved [MCP servers](https://docs.subcloak.com/cloak-proxy/api-client/protocols/mcp/), which Cloak Proxy runs for it in a loop. Use it to try prompts against your API's data, to compare models, to test an MCP server the way an assistant would use it, or to check model output in the collection runner.

## Send a prompt

1. Store your provider's API key in the [vault](https://docs.subcloak.com/cloak-proxy/api-client/variables-and-environments/). New AI requests look for a vault entry named `anthropic_api_key`. Name keys for other providers as you like, for example `openai_api_key`.

2. In the API client, click **New › Other Protocols › AI**, or right-click a collection and choose **Add Other Request › AI**. Search Everything (<kbd>⇧</kbd> <kbd>⌘</kbd> <kbd>A</kbd>) also has **New AI Request**.

3. Choose a provider and a model at the top of the request.

4. Open **Settings** and check the **API key** field references your vault entry, for example `{{vault:openai_api_key}}`.

5. Write your prompt in **Prompt**, and optionally a system prompt in **System**.

6. Press <kbd>⌘</kbd> <kbd>↩</kbd> or click **Send**. The answer streams into **Response**.

> **From 0.4.2, Cloak Proxy asks first:**
> (added in Cloak Proxy 0.4.2) The first time an AI request goes to a provider (from the tab, a collection run, a monitor or a load test), Cloak Proxy asks whether to send it, showing the endpoint and what's sent: the prompt and system prompt with variables filled in, the conversation, and results from attached MCP servers. **Allow** is remembered for the provider; **Cancel** stops the request with "Not sent". Servers on your own computer never ask. `cloakctl collection run` doesn't ask. See [AI providers](https://docs.subcloak.com/cloak-proxy/reference/security-and-privacy/#ai-providers).

(added in Cloak Proxy 0.5.1) The [AI Guard](https://docs.subcloak.com/cloak-proxy/security/ai-security/#ai-guard) checks AI requests for keys and personal data before they're sent, and can warn, redact or block.

*[Screenshot: An AI request with provider Anthropic, model Default (claude-opus-5-5), a Model id field, and the prompt Summarize what this API does in one paragraph, above an empty Response pane and Messages log. A new AI request. The answer streams into Response; the log on the right shows the request and any tool calls.]*

## Providers and models

| Provider | API | Default base URL | API key sent as |
|---|---|---|---|
| **Anthropic** (default) | Messages API | `https://api.anthropic.com` | `x-api-key` header |
| **OpenAI** | Chat Completions | `https://api.openai.com/v1` | `Authorization: Bearer` header |
| **Google Gemini** | `generateContent` | `https://generativelanguage.googleapis.com/v1beta` | `x-goog-api-key` header |
| **OpenAI-compatible** | Chat Completions at a base URL you choose | `http://localhost:11434/v1` (Ollama) | `Authorization: Bearer` header, only when a key is set |

**OpenAI-compatible** covers any server that speaks the Chat Completions format, such as Ollama, LM Studio, vLLM and OpenRouter. Local servers usually need no key.

The model menu offers **Default** (the first model below) and a list of suggestions for the provider:

| Provider | Suggested models |
|---|---|
| **Anthropic** | `claude-opus-5-5`, `claude-sonnet-5-5`, `claude-haiku-5-5`, `claude-opus-4-8`, `claude-sonnet-4-6`, `claude-haiku-4-5` |
| **OpenAI** | `gpt-4.1`, `gpt-4.1-mini`, `gpt-4o`, `gpt-4o-mini`, `o4-mini`, `o3` |
| **Google Gemini** | `gemini-2.5-pro`, `gemini-2.5-flash`, `gemini-2.5-flash-lite` |
| **OpenAI-compatible** | `llama3.2`, `qwen2.5`, `mistral`, `openai/gpt-4o-mini` |

To use any other model, type its id in **Model id (any)**. Whatever you type there is the model the request uses.

Typical setups:

**Anthropic**

| Field | Value |
|---|---|
| Provider | **Anthropic** |
| **API key** | `{{vault:anthropic_api_key}}` |
| **Base URL** | Empty |

**OpenAI**

| Field | Value |
|---|---|
| Provider | **OpenAI** |
| **API key** | `{{vault:openai_api_key}}` |
| **Base URL** | Empty |

**Ollama**

| Field | Value |
|---|---|
| Provider | **OpenAI-compatible** |
| Model | The model you pulled, such as `llama3.2` |
| **API key** | Empty |
| **Base URL** | Empty, or `http://localhost:11434/v1` |

## Request tabs

| Tab | What it holds |
|---|---|
| **Prompt** | The user message. `{{variables}}` are filled in when you send. |
| **System** | An optional system prompt, also with `{{variables}}`. A dot on the tab means it's set. |
| **MCP Tools** | MCP servers whose tools the model may call (see below). The badge counts the selected servers. |
| **Settings** | The key, base URL and generation settings. |

### Settings

| Setting | Default | What it does |
|---|---|---|
| **API key** | `{{vault:anthropic_api_key}}` | The provider key. Type a reference such as `{{vault:openai_api_key}}` rather than the key itself. If the reference can't be resolved, the request is sent without a key and the provider answers with an authentication error. |
| **Base URL (optional; Ollama: http://localhost:11434/v1)** | Empty | Overrides the provider's default base URL. For OpenAI-compatible servers, `/chat/completions` is added to the base URL; a full URL that already ends in `/chat/completions` (optionally with a query string) is used as is. `{{variables}}` work. |
| **Temperature** | 1.0 | Randomness, from 0 to 2. |
| **Max tokens** | 1024 | The longest answer allowed, from 16 to 64,000 in steps of 256. |
| **JSON output** | Off | Asks for a single JSON object: OpenAI-style JSON mode for OpenAI and compatible servers, a JSON response type for Gemini, and an instruction added to the system prompt for Anthropic. |

## Conversations and streaming

In the editor, answers stream token by token into **Response**, with **You** and **Assistant** labels. Each **Send** adds your current prompt to the same conversation, so you can ask follow-up questions; click **New Conversation** to start over. **Stop** cancels an answer in progress.

Next to the **Response** heading, Cloak Proxy shows the latency, the time to the first token, and the input and output token counts the provider reported.

Without MCP tools, the Messages log on the right shows the exact HTTP request sent to the provider as a cURL command, with the API key redacted. Errors appear there as **Request failed**, with the provider's message.

## Give the model MCP tools

An AI request can let the model call tools from MCP servers you've saved as [MCP requests](https://docs.subcloak.com/cloak-proxy/api-client/protocols/mcp/). Cloak Proxy connects to those servers, offers their tools to the model, runs each tool call the model makes, and sends the results back, repeating until the model gives a final answer. This is the same loop an AI assistant runs.

1. Save at least one MCP request in a collection. Requests in the MCP section count.

2. In the AI request, open **MCP Tools**. Every MCP request in your workspace is listed as `name · command or URL`.

3. Check the servers whose tools the model may use.

4. Set **Max steps**, from 1 to 30 (default 8). A step is one call to the model; the loop stops when the model answers without calling a tool, or after this many steps.

5. Send the prompt.

While the loop runs, the log shows how many tools are available, each tool call as `server · tool` with its arguments, and each result as `tool · 140 ms` (in red when the tool reported an error). The answer area shows the model's text from every step.

How the loop works:

- Tools from all selected servers are offered together. When two servers have a tool with the same name, the second is renamed to `server__tool`. Names are made safe for the model's API and kept to 64 characters.
- All tool calls the model makes in one step run at the same time; their results go back in the order the model asked.
- Arguments are checked against the tool's input schema first. Invalid arguments, unknown tools and failed calls are sent back to the model as error results, so it can correct itself.
- A server that fails to list its tools is logged and skipped; the others still work.
- The MCP servers are connected when you send, and closed when the answer is complete.

> **The model calls tools without asking:**
> Cloak Proxy runs every tool call the model makes, without asking you first. Only attach MCP servers whose tools are safe to run unattended, and keep **Max steps** low while you try a new server. An STDIO server is a program running on your Mac with your permissions.

## Run AI requests in the collection runner

The [collection runner](https://docs.subcloak.com/cloak-proxy/api-client/run/collection-runner/) and [`cloakctl`](https://docs.subcloak.com/cloak-proxy/automation/cloakctl/) send a saved AI request as a single, non-streaming call with the **Prompt** as the only user message. With MCP servers selected, they run the tool loop to the end. The run waits at least 60 seconds for the model to start answering, or the request timeout if that's longer.

| Response part | Value |
|---|---|
| Status | `200 OK` when the model answered. Provider errors, such as a bad key or an unknown model, are reported as an error with no response. |
| `Content-Type` | `text/plain; charset=utf-8`, or `application/json` with **JSON output** on. |
| `x-ai-provider` | `openAI`, `anthropic`, `gemini` or `openAICompatible`. |
| `x-ai-model` | The model that answered. |
| `x-ai-input-tokens`, `x-ai-output-tokens` | Token usage, summed over every step of a tool loop, when the provider reports it. |
| `x-ai-stop-reason` | The provider's stop reason, or for a tool loop `finalAnswer` or `maxStepsReached`. |
| Body | The model's final text. With **JSON output** on, a surrounding Markdown code fence is removed so `pm.response.json()` works. |

AI requests have no **Scripts** tab of their own. Put tests in the collection's or a folder's post-response script, and check `pm.info.requestName` for tests meant for one request:

```js title="Collection post-response script"
if (pm.info.requestName === "Classify ticket") {
  pm.test("model returned a known category", () => {
    const answer = pm.response.json();
    pm.expect(["bug", "question", "feature"]).to.include(answer.category);
  });
  pm.test("stayed within budget", () => {
    pm.expect(Number(pm.response.headers.get("x-ai-output-tokens"))).to.be.below(300);
  });
}
```

Model output varies from run to run, even with a low temperature. Test for structure and ranges rather than exact text.

> **Keep API keys in the vault:**
> The **API key** field is saved with the request in your workspace files, so a key typed there ends up in Git and in exports. Store keys in the [vault](https://docs.subcloak.com/cloak-proxy/api-client/variables-and-environments/), which keeps them in your macOS Keychain, and reference them as `{{vault:name}}`. Keys are used only for the call and are redacted in the log. AI requests go straight to the provider, not through Cloak Proxy, and calls to paid providers are billed to your account.

## Related

  - [MCP client](https://docs.subcloak.com/cloak-proxy/api-client/protocols/mcp/): Connect to the MCP servers whose tools you give the model.
  - [Variables and environments](https://docs.subcloak.com/cloak-proxy/api-client/variables-and-environments/): Store API keys in the vault.
  - [Agent mode](https://docs.subcloak.com/cloak-proxy/api-client/agent-mode/): Let an assistant work in the API client for you.
  - [Collection runner](https://docs.subcloak.com/cloak-proxy/api-client/run/collection-runner/): Run AI requests with tests.
