# Models API

List available models and retrieve model information.

## List Models

Get a list of all available models in your workspace.

### Endpoint

```
GET https://api.tokenlio.ai/v1/models
```

### Authentication

```
Authorization: Bearer YOUR_API_KEY
```

### Request

::: code-group

```bash [cURL]
curl https://api.tokenlio.ai/v1/models \
  -H "Authorization: Bearer YOUR_API_KEY"
```

```python [Python]
from openai import OpenAI

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

models = client.models.list()

for model in models.data:
    print(f"{model.id}: {model.owned_by}")
```

```javascript [Node.js]
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'YOUR_API_KEY',
  baseURL: 'https://api.tokenlio.ai/v1',
});

const models = await client.models.list();

for (const model of models.data) {
  console.log(`${model.id}: ${model.owned_by}`);
}
```

:::

### Response

```json
{
  "object": "list",
  "data": [
    {
      "id": "gpt-4-turbo",
      "object": "model",
      "created": 1699999999,
      "owned_by": "openai"
    },
    {
      "id": "gpt-4",
      "object": "model",
      "created": 1699999999,
      "owned_by": "openai"
    },
    {
      "id": "claude-3-5-sonnet",
      "object": "model",
      "created": 1699999999,
      "owned_by": "anthropic"
    }
  ]
}
```

## Retrieve Model

Get detailed information about a specific model.

### Endpoint

```
GET https://api.tokenlio.ai/v1/models/{model_id}
```

### Request

::: code-group

```bash [cURL]
curl https://api.tokenlio.ai/v1/models/gpt-4-turbo \
  -H "Authorization: Bearer YOUR_API_KEY"
```

```python [Python]
model = client.models.retrieve("gpt-4-turbo")
print(f"Model: {model.id}")
print(f"Owner: {model.owned_by}")
```

```javascript [Node.js]
const model = await client.models.retrieve('gpt-4-turbo');
console.log(`Model: ${model.id}`);
console.log(`Owner: ${model.owned_by}`);
```

:::

### Response

```json
{
  "id": "gpt-4-turbo",
  "object": "model",
  "created": 1699999999,
  "owned_by": "openai"
}
```

## Public Models Endpoint

Get public pricing information without authentication.

### Endpoint

```
GET https://api.tokenlio.ai/api/v1/public/models
```

### No Authentication Required

This endpoint is publicly accessible for pricing information.

### Request

```bash
curl https://api.tokenlio.ai/api/v1/public/models
```

### Response

```json
[
  {
    "model_id": "gpt-4-turbo",
    "display_name": "GPT-4 Turbo",
    "provider": "openai",
    "category": "chat",
    "our_price": {
      "input": 0.008,
      "output": 0.024,
      "unit": "per 1M tokens"
    },
    "official_price": {
      "input": 0.01,
      "output": 0.03,
      "source": "openai"
    },
    "savings_percent": 20,
    "capabilities": ["streaming", "function_calling"],
    "context_length": 128000
  }
]
```

### Response Fields

| Field | Type | Description |
|-------|------|-------------|
| `model_id` | string | Model identifier for API calls |
| `display_name` | string | Human-readable model name |
| `provider` | string | Model provider (openai, anthropic, etc.) |
| `category` | string | Model category (chat, embedding, image, etc.) |
| `our_price` | object | Tokenlio pricing |
| `official_price` | object | Official provider pricing (if available) |
| `savings_percent` | number | Percentage saved vs official price |
| `capabilities` | array | Supported features |
| `context_length` | integer | Maximum context window in tokens |

## Model Categories

Models are categorized by their primary function:

### Chat Models

Conversational AI for text generation:
- GPT-4, GPT-3.5
- Claude 3.5, Claude 3
- Gemini 1.5, Gemini 1.0

### Embedding Models

Convert text to vector representations:
- text-embedding-3-small
- text-embedding-3-large
- text-embedding-ada-002

### Image Models

Generate or analyze images:
- DALL-E 3
- DALL-E 2

### Audio Models

Speech-to-text and text-to-speech:
- Whisper
- TTS models

## Model Properties

### Context Length

Maximum tokens the model can process:

| Model | Context Length |
|-------|----------------|
| GPT-4 Turbo | 128,000 |
| GPT-4 | 8,192 |
| Claude 3.5 Sonnet | 200,000 |
| Gemini 1.5 Pro | 1,000,000 |

### Capabilities

Common capabilities:

- **streaming**: Supports token streaming
- **function_calling**: Can call functions/tools
- **json_mode**: Structured JSON output
- **vision**: Can process images

## Usage Examples

### Filter by Provider

```python
models = client.models.list()

openai_models = [m for m in models.data if m.owned_by == "openai"]
anthropic_models = [m for m in models.data if m.owned_by == "anthropic"]

print(f"OpenAI models: {len(openai_models)}")
print(f"Anthropic models: {len(anthropic_models)}")
```

### Check Model Availability

```python
def is_model_available(model_id: str) -> bool:
    try:
        client.models.retrieve(model_id)
        return True
    except Exception:
        return False

if is_model_available("gpt-4-turbo"):
    print("GPT-4 Turbo is available")
```

### Get Pricing Information

```python
import requests

response = requests.get("https://api.tokenlio.ai/api/v1/public/models")
models = response.json()

for model in models:
    if model.get("savings_percent"):
        print(f"{model['display_name']}: Save {model['savings_percent']}%")
```

## Model Identifiers

Use these identifiers in API requests:

### OpenAI

- `gpt-4-turbo`
- `gpt-4`
- `gpt-3.5-turbo`
- `gpt-3.5-turbo-16k`

### Anthropic

- `claude-3-5-sonnet`
- `claude-3-opus`
- `claude-3-sonnet`
- `claude-3-haiku`

### Google

- `gemini-1.5-pro`
- `gemini-1.5-flash`
- `gemini-1.0-pro`

See the [model marketplace](https://api.tokenlio.ai/marketplace) page for the complete list with pricing.

## Error Handling

### Model Not Found

```json
{
  "error": {
    "message": "Model 'invalid-model' not found",
    "type": "invalid_request_error",
    "code": "model_not_found"
  }
}
```

### No Access

```json
{
  "error": {
    "message": "You do not have access to this model",
    "type": "permission_error",
    "code": "model_not_accessible"
  }
}
```

## Best Practices

### Cache Model Lists

Don't fetch the model list on every request:

```python
from functools import lru_cache
from datetime import datetime, timedelta

@lru_cache(maxsize=1)
def get_cached_models():
    return client.models.list()

# Refresh cache periodically
models = get_cached_models()
```

### Validate Model Selection

Check model availability before making requests:

```python
def validate_model(model_id: str) -> bool:
    models = client.models.list()
    available = [m.id for m in models.data]
    return model_id in available

if not validate_model("gpt-4-turbo"):
    raise ValueError("Model not available")
```

### Handle Model Changes

Models may be deprecated or renamed:

```python
MODEL_ALIASES = {
    "gpt-4-0613": "gpt-4",
    "gpt-3.5-turbo-0613": "gpt-3.5-turbo",
}

def resolve_model(model_id: str) -> str:
    return MODEL_ALIASES.get(model_id, model_id)
```

## Next Steps

- [View all models and pricing](https://api.tokenlio.ai/marketplace)
- [Learn to use Chat Completions](/api/chat)
- [Understand billing](/guide/billing)
- [Optimize your requests](/guide/making-requests)

## Need Help?

- [FAQ](/support/faq)
- [Contact Support](/support/contact)
- Email: [support@tokenlio.ai](mailto:support@tokenlio.ai)
