Skip to content
OpenAI logo

GPT-5.1 Chat

Text GenerationOpenAI

GPT-5.1 Chat is the chat-tuned variant of GPT-5.1, optimised for back-and-forth conversation and instruction following.

Model Info
Context Window128,000 tokens
Terms and Licenselink
More informationlink
Request formatsChat Completions
PricingView pricing in the Cloudflare dashboard

Usage

TypeScript
const response = await env.AI.run(
'openai/gpt-5.1-chat',
{ messages: [{ content: 'What are the three laws of thermodynamics?', role: 'user' }] },
)
console.log(response)
The three laws of thermodynamics can be stated simply as:

• **First Law (Law of Energy Conservation):**  
Energy cannot be created or destroyed, only converted from one form to another. The total energy of an isolated system remains constant.

• **Second Law:**  
In any natural process, the total entropy (disorder) of an isolated system always increases. Heat flows spontaneously from hot to cold, not the other way around.

• **Third Law:**  
As the temperature of a system approaches absolute zero, its entropy approaches a minimum value (often taken as zero). Absolute zero cannot be reached in a finite number of steps.

If you want, I can also include the "zeroth" law for completeness.

Examples

With System Message — Using a system message to set context
TypeScript
const response = await env.AI.run(
'openai/gpt-5.1-chat',
{
messages: [
{ content: 'You are a helpful coding assistant specializing in Python.', role: 'system' },
{ content: 'How do I read a JSON file in Python?', role: 'user' },
],
},
)
console.log(response)
The easiest way to read a JSON file in Python is to use the built‑in json module.

Example:

    import json

    with open("data.json", "r") as f:
        data = json.load(f)

    print(data)

Notes:
• json.load reads the file and converts it into Python types (dict, list, etc.).
• Make sure the JSON file contains valid JSON syntax.
Multi-turn Conversation — Continuing a conversation with context
TypeScript
const response = await env.AI.run(
'openai/gpt-5.1-chat',
{
max_completion_tokens: 8192,
messages: [
{
content: 'I need help planning a road trip from San Francisco to Los Angeles.',
role: 'user',
},
{
content:
"I'd be happy to help! The drive is about 380 miles and takes roughly 5-6 hours. Would you like suggestions for scenic routes or interesting stops along the way?",
role: 'assistant',
},
{ content: 'Yes, what are some good places to stop?', role: 'user' },
],
},
)
console.log(response)
Here are some great stops along the way from San Francisco to Los Angeles, depending on which route you choose:

**If you take Highway 1 (the scenic coastal route):**
- **Santa Cruz** – Beach town vibe, boardwalk, coffee shops.
- **Monterey** – Cannery Row, aquarium, beautiful coastal paths.
- **Carmel-by-the-Sea** – Charming shops, galleries, the famous white-sand beach.
- **Bixby Bridge** – Iconic Big Sur photo stop.
- **Pfeiffer Big Sur State Park** – Short hikes, redwoods, river access.
- **McWay Falls** – One of the most photographed waterfalls on the coast.
- **San Simeon** – Hearst Castle tours.
- **Morro Bay** – Seaside village, great for lunch.
- **Pismo Beach** – Long beach, dunes, relaxed atmosphere.
- **Santa Barbara** – Restaurants, beach, Spanish-style architecture.

**If you take Highway 101 (faster, still scenic):**
- **Gilroy** – Garlic capital, outlet shopping.
- **Paso Robles** – Wineries and tasting rooms.
- **San Luis Obispo** – A fun downtown and the Mission.

**If you take I‑5 (fastest route):**
- **Santa Nella** – Classic pit stop, home of Pea Soup Andersen’s.
- **Pyramid Lake** – Quick scenic break before LA.

If you tell me your preferred pace, interests (food, nature, beaches, wineries, towns), or how many days you want for the trip, I can customize a full itinerary.
Creative Writing — Longer completion for creative output
TypeScript
const response = await env.AI.run(
'openai/gpt-5.1-chat',
{
max_completion_tokens: 8192,
messages: [
{
content: 'Write a short story opening about a detective finding an unusual clue.',
role: 'user',
},
],
},
)
console.log(response)
Detective Mara Lorne had seen her share of strange crime scenes, but none as quietly unsettling as the abandoned greenhouse on Cinder Street. The air was warm and sweet, thick with the scent of overgrown jasmine. Glass panes lay shattered underfoot like frozen puddles.

She crouched beside the body, careful not to disturb the latticework of vines that had grown over the floor. That was when she noticed it: an origami bird, perched neatly on the victim’s open palm. It wasn’t the paper creation itself that made her pulse quicken, but the material. The bird was folded not from paper at all, but from a single, translucent petal—still fresh, as though plucked moments ago from a flower that did not exist in any greenhouse Mara had ever seen.
Streaming Response — Enable streaming for real-time output
TypeScript
const response = await env.AI.run(
'openai/gpt-5.1-chat',
{
messages: [{ content: 'Explain the concept of recursion with a simple example.', role: 'user' }],
stream: true,
stream_options: { include_usage: true },
},
)
console.log(response)
Recursion is a programming technique where a function calls itself to solve a smaller version of the same problem. It continues doing this until it reaches a simple stopping point called the base case.

Simple example: counting down.

Imagine a function called countdown(n).  
If n is 0, it stops.  
Otherwise, it prints n and then calls itself with n−1.

In plain steps:
• countdown(3) prints 3, then calls countdown(2)  
• countdown(2) prints 2, then calls countdown(1)  
• countdown(1) prints 1, then calls countdown(0)  
• countdown(0) stops  

This shows how a big task is broken into smaller tasks until it’s simple enough to finish.

Parameters

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
tool_choice
response_format

API Schemas (Raw)

Input
Output