Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

Model run API: Workers Binding, REST Endpoints, and Request Formats

How to call hosted models from a Worker or curl: model IDs, four REST routes, messages vs input, and reasoning fields.

This page summarises Cloudflare model catalog Usage sections: the same hosted model is invoked from a Worker with env.AI.run or over HTTPS with a Cloudflare API token. The model string, JSON body shape, and response fields depend on which API family that model’s page documents—not on the upstream vendor alone.

Workers AI env.AI.run: how to call a model from a Worker

Section titled “Workers AI env.AI.run: how to call a model from a Worker”

From a Cloudflare Worker that has a Workers AI binding, the documented pattern is a single call whose first argument is the catalog model id and whose second argument matches that model’s API family (chat messages, Anthropic messages, Gemini contents, or Responses input).

const response = await env.AI.run('openai/gpt-6.1-sol', { ... })

— Cloudflare, GPT-6.1 Sol

Model ids use a vendor slash catalog name form, for example anthropic/claude-sonnet-5.5, google/gemini-2.5-flash, or xai/grok-4.20-0309-reasoning. The model page for your target is the source of truth for the exact string and option keys. Responses may echo a different model slug (snapshot or provider path) than the id you passed.

REST authentication: curl, account id, and four endpoint families

Section titled “REST authentication: curl, account id, and four endpoint families”

Server-side or local scripts can mirror the same models with the Cloudflare REST API. Examples consistently use:

  • URL base: https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/...
  • Header: Authorization: Bearer $CLOUDFLARE_API_TOKEN
  • Header: Content-Type: application/json
  • Body: JSON including a top-level "model" field that matches the Workers id (Gemini uses an extra input wrapper on /ai/run; see below).
API family REST path Typical Worker options Models illustrated in sources
OpenAI-compatible chat .../ai/v1/chat/completions messages, max_tokens or max_completion_tokens GPT-4.x–6.x, Grok (most), Qwen, MiniMax, Beam, DeepSeek, Ember, o3/o4-mini
Anthropic Messages .../ai/v1/messages messages, max_tokens; optional system Claude Fable/Haiku/Opus/Sonnet pages
Thinking Machines Messages .../ai/v1/messages messages, max_tokens; tools on Inkling 256K Inkling, Inkling 256K
Google Gemini run .../ai/run contents with parts / text Gemini 2.5 Flash and related Gemini pages
OpenAI Responses .../ai/v1/responses input; optional instructions, max_output_tokens, reasoning GPT-5.4/5.5 Pro, 5.6 Luna/Sol/Terra, GPT-6 Astra/Luna/Sol, GPT-6.1 Sol/Terra, Grok 4.20 Multi-Agent

curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/responses

— Cloudflare, GPT-6.1 Sol

curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions

— Cloudflare, GPT-6.1 Sol (Examples)

Common conclusion: pick the endpoint family shown on that model’s Usage section. Using chat messages against a Responses-only example (or the reverse) will not match the documented contract.

OpenAI Chat Completions: messages and token limit field names

Section titled “OpenAI Chat Completions: messages and token limit field names”

Chat Completions is the default surface for many catalog models, including GPT-4.1, DeepSeek V4 Pro, Grok 4.7, and Qwen3.8 Max.

Request shape people copy from model pages

Section titled “Request shape people copy from model pages”

Workers and curl examples use an array of { role, content } (order of keys varies). Token caps appear as max_tokens, max_completion_tokens, or are omitted where the sample uses defaults—for example Beam documents max_completion_tokens with reasoning_effort: 'low', while Pareto uses a tight max_tokens: 16 for one-word answers.

Reasoning-related request fields on chat routes include reasoning_effort: 'none' on GPT-6.1 Terra (Workers and curl) and reasoning_effort: 'low' on Beam.

Response shape: content plus optional reasoning fields

Section titled “Response shape: content plus optional reasoning fields”

Successful chat responses use object: "chat.completion", with assistant text in choices[].message.content. Reasoning-capable chat models often add parallel fields:

  • message.reasoning_content — seen on Beam, Ember-1, Grok 4.20 Reasoning, Qwen3.5, MiniMax M2.7
  • usage.completion_tokens_details.reasoning_tokens (and related detail objects) on several OpenAI, Grok, and o-series samples such as o3-mini
  • MiniMax-specific reasoning_details[] with format: "MiniMax-response-v1" on M2.7

Many responses also include gatewayMetadata.keySource (Unified or BYOK).

OpenAI Responses API: input, instructions, and reasoning.effort

Section titled “OpenAI Responses API: input, instructions, and reasoning.effort”

The Responses route uses a prompt-oriented body, not a messages array in the primary examples on GPT-5.4 Pro, GPT-5.6 Sol, and GPT-6.1 Sol.

A minimal documented body looks like:

{
"model": "openai/gpt-5.4-pro",
"input": "What are the three laws of thermodynamics?"

— pattern from GPT-5.4 Pro

GPT-6.1 Sol adds max_output_tokens and reasoning: { effort: 'high' }. GPT-5.6 Luna examples use instructions for tone or length limits on top of input.

Response shape: output items and reasoning metadata

Section titled “Response shape: output items and reasoning metadata”

Responses return object: "response" with status: "completed" and an output array mixing:

  • type: "reasoning" items (sometimes with encrypted_content in samples)
  • type: "message" items whose content[] includes type: "output_text"

Usage may expose output_tokens_details.reasoning_tokens. GPT-6.1 Sol samples also show store: true, prompt_cache_retention: "24h", and billing.payer: "developer".

Anthropic Messages API: max_tokens, system, and thinking blocks

Section titled “Anthropic Messages API: max_tokens, system, and thinking blocks”

Claude catalog models on Cloudflare call .../ai/v1/messages from Workers and curl—for example Claude Haiku 4.5 and Claude Sonnet 5.5.

"model": "anthropic/claude-haiku-4.5", "max_tokens": 1024, "messages": [{ "content": "...", "role": "user" }]

— structure from Claude Haiku 4.5

Claude Sonnet 5.5 documents an optional top-level system string in both Worker and curl examples. Responses use Anthropic-style type: "message" with content[] text blocks. Thinking models such as Claude Fable 5 and Claude Opus 5.5 add type: "thinking" entries alongside text; Opus 5.5 usage can include output_tokens_details.thinking_tokens.

Do not send Claude models to /ai/v1/chat/completions unless a future model page explicitly documents that path—the cluster sources route Anthropic ids through Messages.

Google Gemini /ai/run: contents, parts, and text

Section titled “Google Gemini /ai/run: contents, parts, and text”

Gemini models use the /ai/run endpoint, not chat messages in Workers snippets such as Gemini 2.5 Flash.

Workers:

env.AI.run('google/gemini-2.5-flash', {
contents: [{ parts: [{ text: '...' }], role: 'user' }],
})

REST curl wraps the same structure under input:

{
"model": "google/gemini-2.5-flash",
"input": {
"contents": [
{ "parts": [{ "text": "..." }], "role": "user" }
]
}
}

— Gemini 2.5 Flash

Answers appear under candidates[].content.parts[].text. Newer Gemini samples may include thoughtSignature on parts and usageMetadata.thoughtsTokenCount. gatewayMetadata.keySource appears as Unified or BYOK in examples across Gemini 3.8 Flash and related pages.

Inkling Messages API: thinking blocks and tool use on 256K

Section titled “Inkling Messages API: thinking blocks and tool use on 256K”

Inkling and Inkling 256K use the same Messages path as Claude (/ai/v1/messages), with max_tokens and string or structured content on each message. Assistant replies interleave type: "thinking" and type: "text" blocks.

Inkling 256K documents tool use: requests may include a tools[] array (name, description, input_schema), assistant messages with type: "tool_use", and user follow-ups with type: "tool_result" linked by tool_use_id. Response model may differ from the catalog id (for example a peft suffix on the 256K variant).

One model, two APIs: when Chat Completions and Responses both appear

Section titled “One model, two APIs: when Chat Completions and Responses both appear”

Some OpenAI catalog entries document both surfaces. GPT-6.1 Sol shows Responses for structured reasoning tasks (input, reasoning.effort, max_output_tokens) and Chat Completions in Examples for migration-style bullet lists (messages, max_completion_tokens). GPT-6 Astra similarly pairs Responses usage with chat messages in examples.

Goal Prefer (per documented examples)
String prompt, reasoning effort, output token cap Responses on the model page
Multi-turn messages, OpenAI client compatibility Chat Completions on the model page
Anthropic Claude Messages
Gemini /ai/run with contents
Grok multi-agent orchestration Responses — Grok 4.20 Multi-Agent
Standard Grok chat variants Chat — compare non-reasoning vs reasoning ids

For GPT-6.1 Terra, chat examples set reasoning_effort: 'none' while Responses examples use reasoning: { effort: 'high' }—the control names differ by API family.

Reasoning and thinking: request controls and response fields

Section titled “Reasoning and thinking: request controls and response fields”
Provider / model (examples) Request control (if shown) Where extra output shows up
Beam reasoning_effort: 'low' reasoning_content, reasoning_tokens in usage
DeepSeek V4 Pro — reasoning_content on message
Ember-1 — reasoning_content, reasoning token details
Anthropic thinking models — thinking content blocks; thinking_tokens on some Opus samples
OpenAI Responses (5.4 Pro+) reasoning: { effort } output reasoning items; reasoning_tokens in usage
GPT-6.1 Terra chat reasoning_effort: 'none' Standard chat completion without the Responses output array
Gemini 2.5+ / 3.x — thoughtsTokenCount, optional thoughtSignature
Grok Reasoning vs Non-Reasoning Model id choice Reasoning id adds reasoning_content and higher reasoning_tokens
MiniMax M2.7 — reasoning_content, reasoning_details
Qwen3.7 Max — Long reasoning_content in chat payload

OpenAI Responses reasoning items in samples may include encrypted_content; treat that as opaque metadata unless your application explicitly consumes it.

gatewayMetadata.keySource: Unified vs BYOK in responses

Section titled “gatewayMetadata.keySource: Unified vs BYOK in responses”

Across Anthropic, Gemini, OpenAI, xAI, MiniMax, and Qwen examples, responses may end with:

"gatewayMetadata": { "keySource": "Unified" }

or "BYOK".

The cluster sources show the field on sample JSON but do not document setup steps here. Practically, the same curl shape can return either value depending on how keys are routed for your account; use it when debugging billing or auth mismatches, not as a request parameter.

OpenAI-compatible clients outside Cloudflare (Perplexity)

Section titled “OpenAI-compatible clients outside Cloudflare (Perplexity)”

Perplexity’s OpenAI SDK compatibility document shows a familiar chat.completion object (model such as sonar-pro) with extra retrieval fields: citations[], search_results[], and detailed usage.cost breakdown.

Common conclusion with Cloudflare chat examples: both ecosystems speak OpenAI-like completion JSON, but Cloudflare-hosted samples emphasise gatewayMetadata and provider-specific reasoning fields, while Perplexity adds search citations. Point your client at the URL and schema your provider documents; do not assume Perplexity fields on Cloudflare responses or vice versa.

Common mistakes when calling hosted models

Section titled “Common mistakes when calling hosted models”
Mistake What the model pages use instead
Gemini with messages contents / parts / text on Gemini 2.5 Flash
Claude on chat completions /ai/v1/messages — Claude Haiku 4.5
Responses model with only messages /ai/v1/responses with input unless the page’s Examples show chat too (GPT-6.1 Sol)
max_tokens on Beam max_completion_tokens plus reasoning_effort in documented samples
Ignoring response model rename Compare returned model to request id (Ember-1 returns a accounts/fireworks/... path)
Picking Grok variant by API only Match id suffix: reasoning vs non-reasoning vs multi-agent Responses
  1. Open the catalog page for your model (for example GPT-6.1 Sol or Claude Sonnet 5.5) and note whether Usage shows /ai/v1/responses, /ai/v1/chat/completions, /ai/v1/messages, or /ai/run.

  2. In a Worker with the AI binding, call env.AI.run with the exact model string from that page and the option keys from the Worker snippet.

  3. Replay the page’s curl example with your $CLOUDFLARE_ACCOUNT_ID and a token that has access to Workers AI.

  4. Confirm the top-level object or type matches the family (response, chat.completion, or Anthropic/Inkling message) and inspect gatewayMetadata.keySource, token usage, and any reasoning_content or thinking blocks you expect for that variant.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page does not match the linked Cloudflare or Perplexity documentation, email yimasynthetic@gmail.com with the page URL and the exact sentence you think is wrong so it can be corrected or clarified.