Skip to main content

A client library for shipping telemetry to the Spyglass AI platform

Project description

Spyglass SDK

The Spyglass SDK provides client code for shipping telemetry data to the Spyglass AI platform

Installation

pip install spyglass-ai

Configuration

Set the following environment variables to configure the SDK:

Required

  • SPYGLASS_API_KEY: Your Spyglass API key
  • SPYGLASS_DEPLOYMENT_ID: Unique identifier for your deployment
    • Note: Used for both service.name and deployment.id attributes

Optional

  • SPYGLASS_OTEL_EXPORTER_OTLP_ENDPOINT: Custom endpoint for development

Example Configuration

export SPYGLASS_API_KEY="your-api-key"
export SPYGLASS_DEPLOYMENT_ID="user-service-v1.2.0"  # Required - used for both service.name and deployment.id

Note: SPYGLASS_DEPLOYMENT_ID is required and will be used for both the OpenTelemetry service.name and deployment.id resource attributes. This ensures consistency and simplifies dashboard queries.

Usage

Basic Function Tracing

Use the @spyglass_trace decorator to automatically trace function calls:

from spyglass_ai import spyglass_trace

@spyglass_trace()
def calculate_total(price, tax_rate):
    return price * (1 + tax_rate)

# Usage
result = calculate_total(100, 0.08)  # This call will be traced

You can also provide a custom span name:

@spyglass_trace(name="payment_processing")
def process_payment(amount, card_info):
    # Payment processing logic
    return {"status": "success", "transaction_id": "tx_123"}

OpenAI Integration

Wrap your OpenAI client to automatically trace API calls:

from openai import OpenAI
from spyglass_ai import spyglass_openai

# Create your OpenAI client
client = OpenAI(api_key="your-api-key")

# Wrap it with Spyglass tracing
client = spyglass_openai(client)

# Now all chat completions will be traced
response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Hello!"}],
    max_tokens=100
)

Complete Example

from openai import OpenAI
from spyglass_ai import spyglass_trace, spyglass_openai

# Set up OpenAI client with tracing
client = OpenAI(api_key="your-api-key")
client = spyglass_openai(client)

@spyglass_trace(name="ai_conversation")
def have_conversation(user_message):
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": user_message}],
        max_tokens=150
    )
    return response.choices[0].message.content

# This will create traces for both the function and the OpenAI API call
answer = have_conversation("What is the capital of France?")
print(answer)

What Gets Traced

Function Tracing (@spyglass_trace)

  • Function name, module, and qualified name
  • Input arguments (excluding self and cls)
  • Return values
  • Execution time
  • Any exceptions that occur

OpenAI Integration (spyglass_openai)

  • Model used
  • Number of messages
  • Request parameters (max_tokens, temperature, etc.)
  • Token usage (prompt, completion, total)
  • Response model
  • Any API errors

Development

Install Dependencies

uv sync --extra test

Run All Tests

# Run all tests
uv run pytest

# Run with coverage
uv run pytest --cov=src --cov-report=term-missing

# Run specific test file
uv run pytest tests/test_trace.py -v

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spyglass_ai-0.1.5.tar.gz (85.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spyglass_ai-0.1.5-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file spyglass_ai-0.1.5.tar.gz.

File metadata

  • Download URL: spyglass_ai-0.1.5.tar.gz
  • Upload date:
  • Size: 85.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spyglass_ai-0.1.5.tar.gz
Algorithm Hash digest
SHA256 1e873477dbb7f6cc02c9c1ee9a219719fcd129f1cbfb2dfe88dc74a335786cfe
MD5 4706572fb0ff900ce7fd0b2275c2c71a
BLAKE2b-256 e3ea4243a40924ef779004b422c07da12283cd7cf90e79f533b79fbe3ed25a86

See more details on using hashes here.

File details

Details for the file spyglass_ai-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: spyglass_ai-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spyglass_ai-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b769eb57b7e98c39ff3fb923fa7c3b049ada4d79e88327251b659d828ba1a8e9
MD5 10fae2eaa92814410092ed14083c1b43
BLAKE2b-256 34a07c5d8ca087f8b868c035979ca3f1ed3dd9659ea5b033aa88bdd22d23de74

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page