---
description: 
globs: 
alwaysApply: false
---
# OpenAI Compatible API Support

## Overview

Task Master now supports OpenAI compatible API endpoints alongside its existing Anthropic Claude support. This allows users to utilize the following types of models:

1. **Claude (Native)**: Using Anthropic's official SDK (original implementation)
2. **OpenAI (Native)**: Using OpenAI's official SDK 
3. **OpenAI Compatible APIs**: Using OpenAI API format with third-party providers, including:
   - Azure OpenAI
   - Ollama
   - Mistral AI
   - Local models (e.g., LM Studio, Ollama)
   - Custom API endpoints that conform to the OpenAI API format

## Configuration

The following environment variables are available for configuring OpenAI and compatible API providers:

```
# OpenAI official API configuration
OPENAI_API_KEY=your_openai_key_here
OPENAI_MODEL=gpt-4o-2024-05-14  # Default model if not specified

# OpenAI-compatible API configuration
OPENAI_COMPATIBLE_API_KEY=your_compatible_api_key
OPENAI_COMPATIBLE_BASE_URL=http://your-api-endpoint/v1
OPENAI_COMPATIBLE_MODEL=llama3-70b  # Model identifier for your compatible API

# Model selection priority
MODEL_PROVIDER=anthropic  # Options: anthropic, openai, openai-compatible, perplexity, auto
```

## Implementation Notes

- The system maintains a provider priority order for automatic fallback:
  1. Selected provider (`MODEL_PROVIDER`)
  2. Anthropic Claude (if `ANTHROPIC_API_KEY` is available)
  3. OpenAI (if `OPENAI_API_KEY` is available)
  4. OpenAI Compatible (if `OPENAI_COMPATIBLE_API_KEY` is available)
  5. Perplexity (if `PERPLEXITY_API_KEY` is available)

- When `MODEL_PROVIDER=auto`, the system tries providers in this order until a working one is found

- Prompts are automatically adapted for each model type, ensuring consistent results regardless of the provider

- Token counting and chunking logic adapts based on the selected provider

## Usage Examples

```bash
# Using OpenAI
MODEL_PROVIDER=openai task-master parse-prd your-prd.txt

# Using OpenAI-compatible provider
MODEL_PROVIDER=openai-compatible task-master expand --id=5 --research
```

## Prompt Conversion 

The system automatically converts prompts between:
- Anthropic Claude format (system prompt + user/assistant message structure)
- OpenAI completion format (ChatCompletion with system/user/assistant roles)
- Perplexity format (similar to OpenAI, with research capability)

## Error Handling

The system provides graceful degradation:
- If a call to the primary model provider fails, it automatically attempts fallback to other configured providers
- Clear error messages specify which provider failed and why
- When all providers fail, a comprehensive error is displayed with troubleshooting steps

## Performance Considerations

Different model providers may have different:
- Response times
- Token limitations
- Cost structures
- Streaming capabilities

The system optimizes based on the selected provider's characteristics.
