> ## Documentation Index
> Fetch the complete documentation index at: https://docs.laozhang.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Models Guide

> Detailed guide for GPT-5.5, GPT-5, GPT-4.1, and o3/o4 reasoning models

## Model Overview

OpenAI is one of the world's leading AI research institutions, offering multiple high-performance large language models. From GPT-5.5 to GPT-5, GPT-4.1, and the reasoning-focused o3/o4 series, OpenAI provides solutions for various scenarios.

<Note>
  **Full Compatibility**: Laozhang API is 100% compatible with OpenAI's official API format. Simply replace `https://api.openai.com/v1` with `https://api2.laozhang.ai/v1` to use it.
</Note>

## Model Classification

### GPT-5.5 Series

<AccordionGroup>
  <Accordion icon="sparkles" title="GPT-5.5">
    **Latest flagship model for complex professional work**

    * **Core Features**:
      * Supports text and image input
      * 1M context window
      * Strong coding and agentic reasoning
      * Excellent multilingual capabilities

    * **Pricing**:
      * Check the console for real-time pricing

    * **Suitable Scenarios**:
      * Complex task handling
      * Image understanding and analysis
      * Long document processing
      * Professional content generation
  </Accordion>

  <Accordion icon="rocket" title="GPT-4.1 Mini">
    **Fast economical model for everyday workloads**

    * **Core Features**:
      * Fast response speed
      * Good general-purpose quality
      * Fast response speed
      * Suitable for cost-sensitive use

    * **Pricing**:
      * Check the console for real-time pricing

    * **Suitable Scenarios**:
      * Daily conversations
      * Batch processing
      * Development and testing
      * Cost-sensitive applications
  </Accordion>
</AccordionGroup>

### GPT-5 / GPT-4.1 Series

<AccordionGroup>
  <Accordion icon="brain" title="GPT-5.5">
    **Current high-performance model with powerful reasoning capabilities**

    * **Core Features**:
      * 1M context window
      * Strong logical reasoning
      * Excellent code understanding
      * Multi-domain knowledge

    * **Pricing**:
      * Check the console for real-time pricing

    * **Suitable Scenarios**:
      * Complex reasoning tasks
      * Code generation and review
      * Academic research
      * Professional consulting
  </Accordion>

  <Accordion icon="file-text" title="GPT-4.1">
    **Classic stable model for production workloads**

    * **Core Features**:
      * 128K context window
      * Outstanding text understanding
      * Creative writing capabilities
      * Accurate information extraction

    * **Pricing**:
      * Check the console for real-time pricing

    * **Suitable Scenarios**:
      * High-quality content creation
      * Important decision support
      * Detailed analysis reports
  </Accordion>
</AccordionGroup>

### o3 / o4 Reasoning Models

<AccordionGroup>
  <Accordion icon="calculator" title="o3-pro">
    **Reasoning-specialized model with PhD-level thinking ability**

    * **Core Features**:
      * Strongest reasoning capabilities
      * Multi-step thinking process
      * Excellent math problem solving
      * Complex logic analysis

    * **Pricing**:
      * Check the console for real-time pricing

    * **Special Limitations**:
      * Does not support streaming output
      * Does not support `system` role
      * Does not support `temperature` parameter

    * **Suitable Scenarios**:
      * Mathematical olympiad problems
      * Scientific research
      * Code algorithm optimization
      * Complex decision analysis
  </Accordion>

  <Accordion icon="bolt" title="o4-mini">
    **Lightweight reasoning model, extreme cost-performance**

    * **Core Features**:
      * Fast reasoning speed
      * current price shown in console than o3-pro
      * Good code and math capabilities
      * Suitable for daily reasoning tasks

    * **Pricing**:
      * Check the console for real-time pricing

    * **Suitable Scenarios**:
      * Daily math problems
      * Code logic optimization
      * Reasoning practice
      * Education and tutoring
  </Accordion>
</AccordionGroup>

### GPT-4o Series (Classic Multimodal)

<AccordionGroup>
  <Accordion icon="message-circle" title="GPT-4o Mini">
    **Classic lightweight multimodal model for legacy compatibility**

    * **Core Features**:
      * Fast response speed
      * Fast response speed
      * Stable performance
      * Suitable for high-frequency calls

    * **Pricing**:
      * Check the console for real-time pricing

    * **Suitable Scenarios**:
      * Simple conversations
      * Content summarization
      * Text translation
      * Customer service bots
  </Accordion>
</AccordionGroup>

## Code Examples

### Basic Text Dialogue

<CodeGroup>
  ```python Python theme={null}
  from openai import OpenAI

  client = OpenAI(
      api_key="YOUR_API_KEY",
      base_url="https://api2.laozhang.ai/v1"
  )

  # Use GPT-5.5
  response = client.chat.completions.create(
      model="gpt-5.5",
      messages=[
          {"role": "system", "content": "You are a helpful assistant"},
          {"role": "user", "content": "Introduce quantum computing"}
      ],
      temperature=0.7,
      max_tokens=1000
  )

  print(response.choices[0].message.content)
  ```

  ```javascript Node.js theme={null}
  import OpenAI from 'openai';

  const openai = new OpenAI({
    apiKey: 'YOUR_API_KEY',
    baseURL: 'https://api2.laozhang.ai/v1'
  });

  // Use GPT-5.5
  const response = await openai.chat.completions.create({
    model: 'gpt-5.5',
    messages: [
      { role: 'system', content: 'You are a helpful assistant' },
      { role: 'user', content: 'Introduce quantum computing' }
    ],
    temperature: 0.7,
    max_tokens: 1000
  });

  console.log(response.choices[0].message.content);
  ```
</CodeGroup>

### Image Understanding

<CodeGroup>
  ```python Python theme={null}
  # Analyze image content
  response = client.chat.completions.create(
      model="gpt-4o",
      messages=[
          {
              "role": "user",
              "content": [
                  {"type": "text", "text": "What's in this image?"},
                  {
                      "type": "image_url",
                      "image_url": {
                          "url": "https://example.com/image.jpg"
                      }
                  }
              ]
          }
      ]
  )

  print(response.choices[0].message.content)
  ```

  ```javascript Node.js theme={null}
  // Analyze image content
  const response = await openai.chat.completions.create({
    model: 'gpt-4o',
    messages: [
      {
        role: 'user',
        content: [
          { type: 'text', text: "What's in this image?" },
          {
            type: 'image_url',
            image_url: {
              url: 'https://example.com/image.jpg'
            }
          }
        ]
      }
    ]
  });

  console.log(response.choices[0].message.content);
  ```
</CodeGroup>

### Long Document Analysis

```python theme={null}
# Read and analyze long documents
with open('document.txt', 'r', encoding='utf-8') as f:
    document = f.read()

response = client.chat.completions.create(
    model="gpt-4o",  # 128K context
    messages=[
        {
            "role": "user",
            "content": f"Please summarize the following document:\n\n{document}"
        }
    ],
    max_tokens=2000
)

print(response.choices[0].message.content)
```

### Creative Writing

```python theme={null}
response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {
            "role": "system",
            "content": "You are a professional novelist"
        },
        {
            "role": "user",
            "content": "Write a 500-word sci-fi short story about time travel"
        }
    ],
    temperature=1.2,  # Higher temperature, more creative
    max_tokens=1500
)

print(response.choices[0].message.content)
```

### Complex Code Review

```python theme={null}
code = """
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)
"""

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "user",
            "content": f"Review this code and suggest optimizations:\n\n{code}"
        }
    ]
)

print(response.choices[0].message.content)
```

### Mathematical Problem Solving

```python theme={null}
# Use O1 series to solve complex math problems
response = client.chat.completions.create(
    model="o3-pro",
    messages=[
        {
            "role": "user",
            "content": """
            Solve: Find all positive integer solutions (x, y, z) satisfying:
            x² + y² = z²
            x + y + z = 1000
            """
        }
    ]
)

print(response.choices[0].message.content)
```

<Warning>
  **O1 Series Special Notes**:

  * Do not support `system` role messages
  * Do not support streaming output
  * Do not support `temperature`, `top_p` and other creativity parameters
  * `max_tokens` defaults to model's maximum value
</Warning>

## Usage Tips

### 1. Choose the Right Model

| Scenario            | Recommended Model               | Reason                                 |
| ------------------- | ------------------------------- | -------------------------------------- |
| Daily conversations | GPT-5.5 or GPT-4o Mini          | Quality-first or cost-effective choice |
| Image understanding | GPT-5.5 or GPT-4o               | Powerful multimodal capabilities       |
| Complex reasoning   | o3-pro                          | PhD-level thinking                     |
| Code generation     | GPT-5.5, Claude Sonnet 4.6      | Strong code understanding              |
| Long documents      | GPT-5.5, Gemini 3.1 Pro Preview | Large context window                   |
| Creative writing    | GPT-5.5                         | Creative expression                    |
| Math problems       | o4-mini or o3-pro               | Strong reasoning capabilities          |

### 2. Optimize Prompts

<AccordionGroup>
  <Accordion icon="wand-sparkles" title="Clear Instructions">
    ✅ **Good Example**:

    ```
    Write a 500-word blog post about healthy eating.
    Requirements:
    1. Include 3 scientific studies
    2. Provide 5 practical recommendations
    3. Casual and easy-to-understand language
    ```

    ❌ **Bad Example**:

    ```
    Write something about healthy eating
    ```
  </Accordion>

  <Accordion icon="list" title="Step-by-Step">
    For complex tasks, break down into multiple steps:

    ```python theme={null}
    # Step 1: Generate outline
    outline_response = client.chat.completions.create(
        model="gpt-5.5",
        messages=[{
            "role": "user",
            "content": "Create an outline for a blog post on healthy eating"
        }]
    )

    # Step 2: Expand each section based on outline
    detail_response = client.chat.completions.create(
        model="gpt-5.5",
        messages=[
            {"role": "user", "content": "Create an outline for a blog post on healthy eating"},
            {"role": "assistant", "content": outline_response.choices[0].message.content},
            {"role": "user", "content": "Expand the first section in detail"}
        ]
    )
    ```
  </Accordion>

  <Accordion icon="code" title="Use Examples">
    Provide examples to help the model understand your needs better:

    ```python theme={null}
    response = client.chat.completions.create(
        model="gpt-5.5",
        messages=[{
            "role": "user",
            "content": """
            Extract project fields from text. Example:
            
            Input: "Project Atlas uses API key group prod and monthly budget 500 USD"
            Output: {"project":"Atlas","key_group":"prod","monthly_budget_usd":500}
            
            Now extract fields from: "Project Orion uses API key group staging and monthly budget 200 USD"
            """
        }]
    )
    ```
  </Accordion>
</AccordionGroup>

### 3. Parameter Tuning

<ParamField path="temperature" type="number" default="1">
  Control randomness of output:

  * `0`: Most deterministic (translation, summarization)
  * `0.7`: Balanced (general dialogue)
  * `1.0-1.5`: More creative (creative writing)
</ParamField>

<ParamField path="max_tokens" type="integer">
  Maximum number of tokens to generate:

  * Short responses: 500-1000
  * Medium responses: 2000-4000
  * Long responses: 8000+
</ParamField>

<ParamField path="top_p" type="number" default="1">
  Nucleus sampling, alternative to temperature:

  * `0.1`: Conservative
  * `0.9`: More diverse
  * Generally use either `temperature` or `top_p`, not both
</ParamField>

<ParamField path="frequency_penalty" type="number" default="0">
  Reduce repetition:

  * `0`: No penalty
  * `0.5-1.0`: Moderate penalty
  * `2.0`: Maximum penalty
</ParamField>

## Cost Optimization

### 1. Choose Cost-Effective Models

<CardGroup cols={2}>
  <Card title="Daily Tasks" icon="message-circle">
    Use **GPT-4.1 Mini** or **GPT-4o Mini** for simple daily tasks

    * Lower cost than flagship models
    * Good quality
    * Faster speed
  </Card>

  <Card title="Reasoning Tasks" icon="calculator">
    Use **o4-mini** instead of o3-pro

    * 80% price reduction
    * Good reasoning capability
    * Suitable for most scenarios
  </Card>
</CardGroup>

### 2. Control Context Length

```python theme={null}
# ❌ Inefficient: Passing too much context
messages = get_all_history()  # May contain hundreds of messages

# ✅ Efficient: Only keep necessary context
messages = [
    {"role": "system", "content": system_prompt},
    *get_recent_messages(5),  # Only recent 5 messages
    {"role": "user", "content": user_input}
]
```

### 3. Set Reasonable max\_tokens

```python theme={null}
# ❌ Wasteful: No token limit set
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[...]
)

# ✅ Economical: Set reasonable limit
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[...],
    max_tokens=500  # Limit output length
)
```

## Error Handling

### Common Errors

<AccordionGroup>
  <Accordion icon="key" title="401: Unauthorized">
    **Cause**: Invalid or missing API Key

    **Solution**:

    ```python theme={null}
    # Check if API Key is correct
    client = OpenAI(
        api_key="YOUR_API_KEY",  # Ensure this is correct
        base_url="https://api2.laozhang.ai/v1"
    )
    ```
  </Accordion>

  <Accordion icon="clock" title="429: Rate Limit">
    **Cause**: Request rate limit exceeded

    **Solution**:

    ```python theme={null}
    import time
    from openai import RateLimitError

    for i in range(3):  # Retry up to 3 times
        try:
            response = client.chat.completions.create(...)
            break
        except RateLimitError:
            wait_time = 2 ** i  # Exponential backoff
            print(f"Rate limited, waiting {wait_time} seconds...")
            time.sleep(wait_time)
    ```
  </Accordion>

  <Accordion icon="circle-alert" title="400: Invalid Request">
    **Cause**: Parameter format error

    **Solution**:

    * Check if model name is correct
    * Verify message format is correct
    * Ensure parameters meet requirements

    ```python theme={null}
    # Correct format
    response = client.chat.completions.create(
        model="gpt-4o",  # Use correct model name
        messages=[
            {"role": "user", "content": "Hello"}  # Correct message format
        ]
    )
    ```
  </Accordion>
</AccordionGroup>

### Retry Mechanism

```python theme={null}
import time
from openai import OpenAI, APIError, RateLimitError, APIConnectionError

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api2.laozhang.ai/v1"
)

def chat_with_retry(messages, max_retries=3):
    for i in range(max_retries):
        try:
            response = client.chat.completions.create(
                model="gpt-4o",
                messages=messages
            )
            return response
        except RateLimitError as e:
            if i < max_retries - 1:
                wait_time = 2 ** i
                print(f"Rate limited, retrying in {wait_time} seconds...")
                time.sleep(wait_time)
            else:
                raise
        except (APIError, APIConnectionError) as e:
            if i < max_retries - 1:
                print(f"Request failed, retrying...")
                time.sleep(1)
            else:
                raise

# Usage
response = chat_with_retry([
    {"role": "user", "content": "Hello"}
])
```

## Streaming Response

For long responses, use streaming output for better user experience:

<CodeGroup>
  ```python Python theme={null}
  # Streaming output
  stream = client.chat.completions.create(
      model="gpt-4o",
      messages=[
          {"role": "user", "content": "Write an article about AI"}
      ],
      stream=True
  )

  print("Generating...", end="")
  for chunk in stream:
      if chunk.choices[0].delta.content:
          print(chunk.choices[0].delta.content, end="", flush=True)
  print()
  ```

  ```javascript Node.js theme={null}
  // Streaming output
  const stream = await openai.chat.completions.create({
    model: 'gpt-4o',
    messages: [
      { role: 'user', content: 'Write an article about AI' }
    ],
    stream: true
  });

  process.stdout.write('Generating...');
  for await (const chunk of stream) {
    const content = chunk.choices[0]?.delta?.content;
    if (content) {
      process.stdout.write(content);
    }
  }
  console.log();
  ```
</CodeGroup>

## Best Practices

1. **Choose the Right Model**
   * Simple tasks → GPT-4o Mini
   * Complex tasks → GPT-4o
   * Reasoning tasks → O1 series

2. **Optimize Prompts**
   * Clear and specific instructions
   * Provide examples
   * Break down complex tasks

3. **Control Costs**
   * Only pass necessary context
   * Set reasonable `max_tokens`
   * Use cost-effective models

4. **Error Handling**
   * Implement retry mechanism
   * Catch and handle different error types
   * Set reasonable timeout

5. **User Experience**
   * Use streaming output
   * Show loading status
   * Provide feedback

## Related Resources

* [Chat Completions API](/en/api-reference/chat-completions) - Complete API documentation
* [Claude Models](/en/api-reference/claude) - Anthropic Claude models guide
* [Gemini Models](/en/api-reference/gemini) - Google Gemini models guide
* [Pricing](/en/pricing) - Detailed model pricing information
