Palantyra

API Reference

Complete API reference for Palantyra Python SDK

API Reference

Core Functions

initialize()

Initialize the Palantyra SDK.

palantyra.initialize(
    project_api_key: str,           # Required: Your API key
    endpoint: str = None,            # Palantyra server URL
    service_name: str = "llm-app",  # Service identifier
    service_version: str = "1.0.0",  # Service version
    auto_instrument: bool = True,    # Auto-trace LLM calls
    use_batching: bool = True,       # Batch exports
    **exporter_kwargs                # Additional config
)

@observe()

Decorator for automatic function tracing.

@palantyra.observe(
    name: str = None,                # Span name (default: function name)
    session_id: str = None,          # Session identifier
    user_id: str = None,              # User identifier
    metadata: dict = None,            # Custom metadata
    tags: List[str] = None,          # Tags for categorization
    ignore_input: bool = False,       # Don't log inputs
    ignore_output: bool = False,      # Don't log outputs
    ignore_inputs: List[str] = None  # Specific args to ignore
)

start_as_current_span()

Context manager for manual tracing.

with palantyra.start_as_current_span(
    name: str,                       # Required: Span name
    input: Any = None,               # Input data
    span_type: str = "DEFAULT",      # Span type
    session_id: str = None,          # Session ID
    user_id: str = None,              # User ID
    metadata: dict = None,            # Metadata
    tags: List[str] = None           # Tags
):
    # Your code here

Trace Manipulation

set_trace_session_id()

palantyra.set_trace_session_id(session_id: str)

Set session ID for the current trace.

set_trace_user_id()

palantyra.set_trace_user_id(user_id: str)

Set user ID for the current trace.

set_trace_metadata()

palantyra.set_trace_metadata(**metadata)

Set metadata for the current trace.

set_span_output()

palantyra.set_span_output(output: Any)

Set output for the current span.

set_span_attributes()

palantyra.set_span_attributes(attributes: Dict[str, Any])

Set custom attributes on the current span.

Utilities

CostCalculator

from palantyra.utils import CostCalculator

cost = CostCalculator.calculate_cost(
    model="gpt-4",
    input_tokens=100,
    output_tokens=50,
    provider="openai"
)

SDKConfig

from palantyra.utils import SDKConfig

# From environment variables
config = SDKConfig.from_env()

# Manual configuration
config = SDKConfig(
    project_api_key="your-key",
    endpoint="http://localhost:8080",
    service_name="my-app"
)