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

# Python SDK Usage

> Endpoint-by-endpoint examples using the Python SDK.

## Setup

```python theme={null}
from ai_stats import AIStats

client = AIStats(api_key="your-api-key")
```

## Chat completions

```python theme={null}
completion = client.generate_text(
    {
        "model": "openai/gpt-4o-mini",
        "messages": [{"role": "user", "content": "Hi!"}],
    }
)

for line in client.stream_text(
    {
        "model": "openai/gpt-4o-mini",
        "messages": [{"role": "user", "content": "Stream a story"}],
    }
):
    print(line)
```

## Responses

```python theme={null}
resp = client.generate_response(
    {
        "model": "openai/gpt-4.1",
        "input": [{"role": "user", "content": [{"type": "output_text", "text": "Summarise"}]}],
    }
)

for line in client.stream_response(
    {
        "model": "openai/gpt-4.1",
        "input": [{"role": "user", "content": [{"type": "output_text", "text": "Stream it"}]}],
    }
):
    print(line)
```

## Messages (Anthropic-compatible)

```python theme={null}
message = client.generate_message(
    {
        "model": "anthropic/claude-3-5-sonnet-latest",
        "messages": [{"role": "user", "content": "Hello from messages API"}],
        "max_tokens": 128,
    }
)

for line in client.stream_messages(
    {
        "model": "anthropic/claude-3-5-sonnet-latest",
        "messages": [{"role": "user", "content": "Stream a short hello"}],
        "max_tokens": 128,
        "stream": True,
    }
):
    print(line)
```

## Embeddings and moderations

```python theme={null}
embedding = client.generate_embedding(
    {"model": "openai/text-embedding-3-large", "input": "Sample text"}
)

moderation = client.generate_moderation(
    {"model": "openai/omni-moderation-latest", "input": "Text to check"}
)
```

## Models and health

```python theme={null}
models = client.get_models()
health = client.get_health()
```

## Control-plane helpers

```python theme={null}
providers = client.list_providers()
credits = client.get_credits({"team_id": "your-team-id"})
activity = client.get_activity({"team_id": "your-team-id", "days": 30})
```

## Images and audio

```python theme={null}
image = client.generate_image(
    {"model": "openai/gpt-image-1", "prompt": "A lighthouse at golden hour"}
)

transcription = client.generate_transcription(
    {"model": "openai/gpt-4o-transcribe", "audio_b64": "base64-audio"}
)

speech = client.generate_speech(
    {"model": "openai/gpt-4o-mini-tts", "input": "Hello world"}
)
```

## Batches

```python theme={null}
batch = client.create_batch(
    {
        "endpoint": "responses",
        "input_file_id": "file_123",
        "completion_window": "24h",
        "session_id": "agent-run-42",
    }
)

status = client.get_batch(batch["id"])
```

## Files, generations, and video jobs

```python theme={null}
uploaded = client.upload_file(
    purpose="batch",
    file="data:application/json;base64,eyJ0ZXN0Ijp0cnVlfQ==",
)

generation = client.get_generation("G-01ABC...")

job = client.generate_video(
    {"model": "openai/sora-2", "prompt": "A mountain sunrise"}
)
```

## Error handling

```python theme={null}
try:
    client.generate_text({"model": "invalid", "messages": []})
except Exception as exc:
    print(f"API error: {exc}")
```

## Configuration options

```python theme={null}
from ai_stats import AIStats

client = AIStats(
    api_key="your-api-key",
    base_url="https://api.phaseo.app/v1",
    timeout=30.0,
)
```
