---
title: "OpenAI Integration"
source: https://docs.autohand.ai/integrations/openai
---

# OpenAI

Connect directly to OpenAI's API for access to GPT-4o, GPT-4 Turbo, and o1 reasoning models. The industry standard for AI-powered code assistance.

## Overview

OpenAI provides some of the most capable AI models available. When integrated with Autohand, you get:

-   Access to GPT-4o, GPT-4 Turbo, and o1 reasoning models
-   Direct API connection for lowest latency
-   Vision capabilities for image understanding
-   Function calling for structured outputs
-   Reliable infrastructure with high uptime

**Alternative:** If you want access to OpenAI models along with other providers, consider using [OpenRouter](/docs/integrations/openrouter.html) which provides a unified API for multiple model providers.

## Setup

Get started with OpenAI in a few steps.

### Get your API key

1.  Go to [platform.openai.com](https://platform.openai.com) and sign in
2.  Navigate to **API Keys** in your account settings
3.  Click **Create new secret key** and copy it

### Configure Autohand

``` bash
# Set environment variable (recommended)
export OPENAI_API_KEY="sk-xxxxxxxxxxxxxxxxxxxx"

# Or configure via CLI
autohand config set openai.apiKey "sk-xxxxxxxxxxxxxxxxxxxx"
```

Verify your configuration:

``` bash
# Start with OpenAI provider
autohand --provider openai --model gpt-4o

# Test with a prompt
autohand --prompt "Hello, which model are you?"
```

## CLI configuration

Configure OpenAI in your `~/.autohand/config.json`:

``` json
{
  "provider": "openai",
  "openai": {
    "apiKey": "${OPENAI_API_KEY}",
    "model": "gpt-4o",
    "maxTokens": 4096,
    "temperature": 0.7,
    "organization": "org-xxxxx"
  }
}
```

### Configuration options

| Option | Description | Default |
|---|---|---|
| apiKey | Your OpenAI API key | - |
| model | Model to use | gpt-4o |
| maxTokens | Maximum tokens in response | 4096 |
| temperature | Response randomness (0-2) | 0.7 |
| organization | OpenAI organization ID | - |
| baseUrl | Custom API endpoint | https://api.openai.com/v1 |

## Available models

OpenAI offers several model tiers optimized for different tasks.

### GPT-4o series

| Model | Context | Best for |
|---|---|---|
| gpt-4o | 128K | Most capable, multimodal |
| gpt-4o-mini | 128K | Fast and cost-effective |

### GPT-4 Turbo

| Model | Context | Best for |
|---|---|---|
| gpt-4-turbo | 128K | Complex reasoning |
| gpt-4-turbo-preview | 128K | Latest features |

### o1 reasoning models

| Model | Context | Best for |
|---|---|---|
| o1 | 200K | Advanced reasoning, complex problems |
| o1-mini | 128K | Fast reasoning tasks |

### Switch models

``` bash
# Set default model
autohand config set model "gpt-4o"

# Use a specific model for one session
autohand --model "o1"

# Switch during a session
/model openai/gpt-4o-mini
```

## Best practices

-   **Use environment variables**: Never commit API keys to version control.
-   **Choose the right model**: Use gpt-4o for most tasks, o1 for complex reasoning.
-   **Monitor usage**: Check your OpenAI dashboard for usage and costs.
-   **Set spending limits**: Configure usage limits in OpenAI dashboard.

### Recommended configurations

#### General coding

``` json
{
  "openai": {
    "model": "gpt-4o",
    "temperature": 0.3,
    "maxTokens": 4096
  }
}
```

#### Complex reasoning

``` json
{
  "openai": {
    "model": "o1",
    "maxTokens": 8192
  }
}
```

## Resources

-   [OpenAI Website](https://openai.com)
-   [API Documentation](https://platform.openai.com/docs)
-   [API Reference](https://platform.openai.com/docs/api-reference)
-   [API Keys Dashboard](https://platform.openai.com/api-keys)
-   [Pricing](https://openai.com/api/pricing)
-   [Status Page](https://status.openai.com)

## Troubleshooting

### Common issues

| Issue | Solution |
|---|---|
| Invalid API key | Verify key at platform.openai.com |
| Rate limited | Wait or upgrade your plan |
| Context too long | Reduce input or use a model with larger context |
| Model not available | Check model access in your OpenAI account |

### Debug mode

``` bash
# Enable verbose logging
AUTOHAND_DEBUG=true autohand --provider openai

# Check API status
curl https://status.openai.com/api/v2/status.json
```