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

# Python SDK Configuration

> Traceloop.init() parameters, environment variables, custom exporters, and pricing config

## Basic Setup

Initialize the SDK with your app name:

```python theme={null}
from anyway.sdk import Traceloop

Traceloop.init(app_name="my-app")
```

## Collector Configuration

Configure the Anyway collector endpoint to send your traces.

### Environment Variables (Recommended)

Set environment variables to configure the SDK:

```bash theme={null}
# Collector endpoint (gRPC - without http/https prefix)
export TRACELOOP_BASE_URL="https://collector.anyway.sh/"

# Authentication headers (URL-encoded)
export TRACELOOP_HEADERS="Authorization=your-api-key"
```

<Note>
  For gRPC endpoints, omit the `http://` or `https://` prefix. For HTTP endpoints, include the full URL with prefix.
</Note>

### Direct Configuration

You can also pass configuration directly to `init()`:

```python theme={null}
from anyway.sdk import Traceloop

Traceloop.init(
    app_name="my-app",
    api_endpoint="https://collector.anyway.sh/",
    headers={"Authorization": "your-api-key"}
)
```

<Warning>
  Avoid hardcoding API keys in your source code. Use environment variables or a secrets manager.
</Warning>

## Configuration Options

| Parameter           | Type     | Description                                               |
| ------------------- | -------- | --------------------------------------------------------- |
| `app_name`          | string   | **Required.** Name of your application for trace grouping |
| `api_endpoint`      | string   | Collector endpoint URL. Overrides `TRACELOOP_BASE_URL`    |
| `headers`           | dict     | Authentication headers. Overrides `TRACELOOP_HEADERS`     |
| `exporter`          | Exporter | Custom OpenTelemetry exporter for advanced use cases      |
| `pricing_json_path` | string   | Path to a custom pricing JSON file for cost calculations  |
| `disable_batch`     | boolean  | Disable batching of traces (useful for debugging)         |

## Custom Exporters

For advanced use cases, you can provide a custom OpenTelemetry exporter:

```python theme={null}
from anyway.sdk import Traceloop
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter

custom_exporter = OTLPSpanExporter(
    endpoint="your-collector:4317",
    headers={"Authorization": "Bearer your-key"}
)

Traceloop.init(
    app_name="my-app",
    exporter=custom_exporter
)
```

This allows integration with any OpenTelemetry-compatible backend.

## Custom Pricing

You can provide your own pricing data to calculate costs for models not included in the default pricing or to override existing prices:

```python theme={null}
from anyway.sdk import Traceloop

Traceloop.init(
    app_name="support-agent",
    api_endpoint="http://your-collector:4318",
    headers={"Authorization": "your-api-key"},
    pricing_json_path="./pricing.json",
    disable_batch=True,
)
```

### Default Pricing

If no custom pricing JSON is provided, Anyway uses a [default pricing file](https://github.com/anyway-platform/anyway-tracing/blob/main/packages/anyway-sdk/anyway/sdk/pricing/data/default_pricing.json) that includes pricing for popular models from OpenAI, Anthropic, Google, Meta, and other providers.

### Pricing JSON Format

The pricing JSON file should follow this structure:

```json theme={null}
{
  "chat": {
    "model-name": {
      "promptPrice": 0.00015,
      "completionPrice": 0.0006
    }
  }
}
```

* `promptPrice`: Cost per 1K input tokens
* `completionPrice`: Cost per 1K output tokens

Example with multiple models:

```json theme={null}
{
  "chat": {
    "gpt-4o-mini": {
      "promptPrice": 0.00015,
      "completionPrice": 0.0006
    },
    "custom-model": {
      "promptPrice": 0.0002,
      "completionPrice": 0.008
    }
  }
}
```

## Environment-Based Configuration

A common pattern is to configure based on environment:

```python theme={null}
import os
from anyway.sdk import Traceloop

env = os.getenv("ENVIRONMENT", "development")

Traceloop.init(
    app_name=f"my-app-{env}",
    api_endpoint=os.getenv("TRACELOOP_BASE_URL"),
    headers={"Authorization": f"Bearer {os.getenv('ANYWAY_API_KEY')}"}
)
```

## Next Steps

<CardGroup cols={2}>
  <Card title="Tracing LLM Calls" icon="route" href="/sdk/python/tracing">
    Trace OpenAI and Anthropic API calls
  </Card>
</CardGroup>
