Skip to main content

Basic Setup

Initialize the SDK with your app name:
from anyway.sdk import Traceloop

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

Collector Configuration

Configure the Anyway collector endpoint to send your traces. Set environment variables to configure the SDK:
# 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"
For gRPC endpoints, omit the http:// or https:// prefix. For HTTP endpoints, include the full URL with prefix.

Direct Configuration

You can also pass configuration directly to init():
from anyway.sdk import Traceloop

Traceloop.init(
    app_name="my-app",
    api_endpoint="https://collector.anyway.sh/",
    headers={"Authorization": "your-api-key"}
)
Avoid hardcoding API keys in your source code. Use environment variables or a secrets manager.

Configuration Options

ParameterTypeDescription
app_namestringRequired. Name of your application for trace grouping
api_endpointstringCollector endpoint URL. Overrides TRACELOOP_BASE_URL
headersdictAuthentication headers. Overrides TRACELOOP_HEADERS
exporterExporterCustom OpenTelemetry exporter for advanced use cases
pricing_json_pathstringPath to a custom pricing JSON file for cost calculations
disable_batchbooleanDisable batching of traces (useful for debugging)

Custom Exporters

For advanced use cases, you can provide a custom OpenTelemetry exporter:
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:
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 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:
{
  "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:
{
  "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:
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

Tracing LLM Calls

Trace OpenAI and Anthropic API calls