GPT-4.1·续
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"annotations": [],
"content": "Detective Lila Maren knelt beside the wrought-iron fence, her breath misting in the pale dawn light. The alley was quiet, save for the distant hum of the city waking. She scanned the scuffed cobblestones, expecting the usual—a cigarette butt, perhaps, or a scrap of torn fabric. Instead, nestled in a shallow puddle, she found a single red chess pawn, oddly pristine amid the grime.\n\nLila lifted it with gloved fingers, turning it over as she studied its glossy sheen. No markings. No blood. Just a chess piece, out of place and glinting like a secret. She frowned, recalling the message scrawled on the victim’s mirror upstairs: “Checkmate.” The game, it seemed, was just beginning.",
"refusal": null,
"role": "assistant"
}
}
],
"created": 1776470819,
"gatewayMetadata": {
"keySource": "Unified"
},
"id": "chatcmpl-DVnV5b5ltpItctts03EY2jcCCWLEe",
"model": "gpt-4.1-2025-04-14",
"object": "chat.completion",
"service_tier": "default",
"system_fingerprint": "fp_7a56a8eabf",
"usage": {
"completion_tokens": 159,
"completion_tokens_details": {
"accepted_prediction_tokens": 0,
"audio_tokens": 0,
"reasoning_tokens": 0,
"rejected_prediction_tokens": 0
},
"prompt_tokens": 20,
"prompt_tokens_details": {
"audio_tokens": 0,
"cached_tokens": 0
},
"total_tokens": 179
}
}
</details>
<details>
<summary>Streaming Response — Enable streaming for real-time output</summary>
const response = await env.AI.run(
'openai/gpt-4.1',
{
messages: [{ content: 'Explain the concept of recursion with a simple example.', role: 'user' }],
stream: true,
stream_options: { include_usage: true },
},
)
console.log(response)
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"model": "openai/gpt-4.1",
"messages": [
{
"content": "Explain the concept of recursion with a simple example.",
"role": "user"
}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
**Recursion** is a programming technique where a function calls itself to solve a problem. Each time the function calls itself, it breaks the problem down into smaller subproblems. For recursion to stop, a **base case** (or termination condition) is required.
**Simple Example: Calculating Factorial**
The **factorial** of a non-negative integer `n` (written as `n!`) is the product of all positive integers less than or equal to `n`.
**Mathematically:**
- `n! = n × (n-1) × (n-2) × ... × 1`
- By definition, `0! = 1`
**Recursive Definition:**
- If `n == 0`, then `factorial(n) = 1` (base case)
- If `n > 0`, then `factorial(n) = n × factorial(n-1)` (recursive case)
**Code Example in Python:**
```python
def factorial(n):
if n == 0: # Base case
return 1
else:
return n * factorial(n - 1) # Recursive call
```
**How it works:**
To compute `factorial(4)`:
- `factorial(4)` returns `4 * factorial(3)`
- `factorial(3)` returns `3 * factorial(2)`
- `factorial(2)` returns `2 * factorial(1)`
- `factorial(1)` returns `1 * factorial(0)`
- `factorial(0)` returns `1` (base case)
So,
- `factorial(1)` = 1 × 1 = 1
- `factorial(2)` = 2 × 1 = 2
- `factorial(3)` = 3 × 2 = 6
- `factorial(4)` = 4 × 6 = 24
**In summary:**
Recursion is when a function calls itself, and the problem gets divided into smaller parts until reaching a simple, solvable case (the base case).
[
{
"choices": [
{
"delta": {
"content": "",
"refusal": null,
"role": "assistant"
},
"finish_reason": null,
"index": 0,
"logprobs": null
}
],
"created": 1776470822,
"id": "chatcmpl-DVnV8rG1ILDUR9r3HsKDVd4dbGH0A",
"model": "gpt-4.1-2025-04-14",
"obfuscation": "nqlUcqwR28bhP",
"object": "chat.completion.chunk",
"service_tier": "default",
"system_fingerprint": "fp_51f6d1255b",
"usage": null
},
{
"choices": [
{
"delta": {
"content": "**"
},
"finish_reason": null,
"index": 0,
"logprobs": null
}
],
"created": 1776470822,
"id": "chatcmpl-DVnV8rG1ILDUR9r3HsKDVd4dbGH0A",
"model": "gpt-4.1-2025-04-14",
"obfuscation": "F8I8qrGpbmKbK",
"object": "chat.completion.chunk",
"service_tier": "default",
"system_fingerprint": "fp_51f6d1255b",
"usage": null
},
"... 440 more chunks omitted ...",
{
"choices": [],
"created": 1776470822,
"id": "chatcmpl-DVnV8rG1ILDUR9r3HsKDVd4dbGH0A",
"model": "gpt-4.1-2025-04-14",
"obfuscation": "qHFF5gBP2hPp",
"object": "chat.completion.chunk",
"service_tier": "default",
"system_fingerprint": "fp_51f6d1255b",
"usage": {
"completion_tokens": 440,
"completion_tokens_details": {
"accepted_prediction_tokens": 0,
"audio_tokens": 0,
"reasoning_tokens": 0,
"rejected_prediction_tokens": 0
},
"prompt_tokens": 17,
"prompt_tokens_details": {
"audio_tokens": 0,
"cached_tokens": 0
},
"total_tokens": 457
}
}
]
</details>
<details>
<summary>Web Search — Letting the model use OpenAI's built-in web search tool to answer with current information</summary>
const response = await env.AI.run(
'openai/gpt-4.1',
{
input: 'What were the top news stories about Cloudflare this week? Summarise in three bullets.',
max_output_tokens: 4096,
tools: [{ type: 'web_search_preview' }],
},
)
console.log(response)
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/responses \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"model": "openai/gpt-4.1",
"input": "What were the top news stories about Cloudflare this week? Summarise in three bullets.",
"max_output_tokens": 4096,
"tools": [
{
"type": "web_search_preview"
}
]
}'
Here are the top news stories about Cloudflare this week:
- **Launch of New Partner Initiative for AI and SASE Adoption**: Cloudflare introduced the Cloudflare One Design Partner designation and an AI-powered toolkit to accelerate the adoption of artificial intelligence (AI) and secure access service edge (SASE) architectures. This initiative aims to help organizations modernize legacy security infrastructures and overcome challenges like configuration risks and lengthy deployment times. ([itpro.com](https://www.itpro.com/technology/artificial-intelligence/cloudflare-launches-new-partner-initiative-to-support-ai-and-sase-adoption?utm_source=openai))
- **Completion of Agent Infrastructure Stack with Browser Run Rebuild**: Cloudflare completed its agent infrastructure stack by rebuilding Browser Run on its own Containers platform. This upgrade delivers higher concurrency, faster response times, and support for WebGL and WebMCP, enhancing the performance and scalability of AI agents. ([infoq.com](https://www.infoq.com/news/2026/05/cloudflare-agent-platform-stack/?utm_source=openai))
- **Workforce Reduction Amid AI Adoption**: Cloudflare announced plans to reduce its global workforce by about 20%, cutting more than 1,100 jobs. This restructuring is part of the company's shift towards an AI-first operating model, reflecting the rapid adoption of AI tools and the resulting changes in operational processes. ([business-standard.com](https://www.business-standard.com/technology/tech-news/cloudflare-to-cut-about-20-workforce-as-ai-adoption-reshapes-operations-126050800117_1.html?utm_source=openai))
{
"id": "resp_0fdf05943aa0606d016a398f3d52b48198b359c259da57fcf2",
"object": "response",
"created_at": 1782157117,
"model": "gpt-4.1-2025-04-14",
"output": [
{
"id": "ws_0fdf05943aa0606d016a398f3db07c8198a82f9fb39e0ca765",
"type": "web_search_call",
"status": "completed",
"action": {
"type": "search",
"queries": [
"Cloudflare top news stories this week"
],
"query": "Cloudflare top news stories this week"
}
},
{
"id": "msg_0fdf05943aa0606d016a398f3f38808198be80ca5b78103aaf",
"type": "message",
"status": "completed",
"content": [
{
"type": "output_text",
"annotations": [
{
"type": "url_citation",
"end_index": 658,
"start_index": 494,
"title": "Cloudflare launches new partner initiative to support AI and SASE adoption",
"url": "https://www.itpro.com/technology/artificial-intelligence/cloudflare-launches-new-partner-initiative-to-support-ai-and-sase-adoption?utm_source=openai"
},
{
"type": "url_citation",
"end_index": 1098,
"start_index": 998,
"title": "Cloudflare Completes Its Agent Infrastructure Stack with Browser Run Rebuild and Six-Layer Platform - InfoQ",
"url": "https://www.infoq.com/news/2026/05/cloudflare-agent-platform-stack/?utm_source=openai"
},
{
"type": "url_citation",
"end_index": 1618,
"start_index": 1427,
"title": "Cloudflare to cut about 20% workforce as AI adoption reshapes operations | Tech News - Business Standard",
"url": "https://www.business-standard.com/technology/tech-news/cloudflare-to-cut-about-20-workforce-as-ai-adoption-reshapes-operations-126050800117_1.html?utm_source=openai"
}
],
"logprobs": [],
"text": "Here are the top news stories about Cloudflare this week:\n\n- **Launch of New Partner Initiative for AI and SASE Adoption**: Cloudflare introduced the Cloudflare One Design Partner designation and an AI-powered toolkit to accelerate the adoption of artificial intelligence (AI) and secure access service edge (SASE) architectures. This initiative aims to help organizations modernize legacy security infrastructures and overcome challenges like configuration risks and lengthy deployment times. ([itpro.com](https://www.itpro.com/technology/artificial-intelligence/cloudflare-launches-new-partner-initiative-to-support-ai-and-sase-adoption?utm_source=openai))\n\n- **Completion of Agent Infrastructure Stack with Browser Run Rebuild**: Cloudflare completed its agent infrastructure stack by rebuilding Browser Run on its own Containers platform. This upgrade delivers higher concurrency, faster response times, and support for WebGL and WebMCP, enhancing the performance and scalability of AI agents. ([infoq.com](https://www.infoq.com/news/2026/05/cloudflare-agent-platform-stack/?utm_source=openai))\n\n- **Workforce Reduction Amid AI Adoption**: Cloudflare announced plans to reduce its global workforce by about 20%, cutting more than 1,100 jobs. This restructuring is part of the company's shift towards an AI-first operating model, reflecting the rapid adoption of AI tools and the resulting changes in operational processes. ([business-standard.com](https://www.business-standard.com/technology/tech-news/cloudflare-to-cut-about-20-workforce-as-ai-adoption-reshapes-operations-126050800117_1.html?utm_source=openai)) "
}
],
"role": "assistant"
}
],
"status": "completed",
"usage": {
"input_tokens": 320,
"output_tokens": 343,
"total_tokens": 663,
"input_tokens_details": {
"cached_tokens": 0
},
"output_tokens_details": {
"reasoning_tokens": 0
}
},
"background": false,
"billing": {
"payer": "developer"
},
"completed_at": 1782157123,
"error": null,
"frequency_penalty": 0,
"incomplete_details": null,
"instructions": null,
"max_output_tokens": 4096,
"max_tool_calls": null,
"moderation": null,
"parallel_tool_calls": true,
"presence_penalty": 0,
"previous_response_id": null,
"prompt_cache_key": null,
"prompt_cache_retention": "in_memory",
"reasoning": {
"context": null,
"effort": null,
"summary": null
},
"safety_identifier": null,
"service_tier": "default",
"store": false,
"temperature": 1,
"text": {
"format": {
"type": "text"
},
"verbosity": "medium"
},
"tool_choice": "auto",
"tools": [
{
"type": "web_search_preview",
"search_content_types": [
"text"
],
"search_context_size": "medium",
"user_location": {
"type": "approximate",
"city": null,
"country": "US",
"region": null,
"timezone": null
}
}
],
"top_logprobs": 0,
"top_p": 1,
"truncation": "disabled",
"user": null,
"metadata": {},
"gatewayMetadata": {
"keySource": "Unified"
}
}
</details>
Parameters
Schema variant
ResponsesChat Completions
▶input
one ofrequired
instructions
string
temperature
numberminimum: 0maximum: 2
max_output_tokens
numberexclusiveMinimum: 0
top_p
numberminimum: 0maximum: 1
stream
boolean
▶tools[]
array
tool_choice
▶text{}
object
▶reasoning{}
object
▶messages[]
arrayrequired
temperature
numberminimum: 0maximum: 2
max_tokens
numberexclusiveMinimum: 0
max_completion_tokens
numberexclusiveMinimum: 0
top_p
numberminimum: 0maximum: 1
frequency_penalty
numberminimum: -2maximum: 2
presence_penalty
numberminimum: -2maximum: 2
stream
boolean
▶stream_options{}
object
▶tools[]
array
tool_choice
response_format
▶modalities[]
array
▶audio{}
object
reasoning_effort
stringOptional reasoning control; availability and accepted values are model-dependent.
id
string
object
stringconst: response
created_at
number
model
string
▶output[]
array
output_text
string
status
stringenum: in_progress, completed, failed, incomplete
▶usage{}
object
id
string
object
string
created
number
model
string
▶choices[]
array
▶usage{}
object
API Schemas (Raw)
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/ai/models/openai/gpt-4.1/#page","headline":"GPT-4.1","description":"OpenAI's flagship GPT model for complex tasks with a million-token context window.","url":"https://developers.cloudflare.com/ai/models/openai/gpt-4.1/","inLanguage":"en","image":"https://developers.cloudflare.com/og-docs.png","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
description: Fast, affordable version of GPT-4.1 with a million-token context window. title: GPT-4.1 mini image: https://developers.cloudflare.com/og-docs.png
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/llms.txt
Use this file to discover all available pages before exploring further.

