Official Python SDK for ingesting live traffic events to Surfa Analytics.
- π Event Buffering - Automatic batching with configurable buffer size
- π Auto-Retry - Built-in retry logic with exponential backoff
- π¦ Context Manager - Automatic session lifecycle management
- π·οΈ Runtime Metadata - Track AI provider, model, and configuration
- β Event Validation - Client-side validation before sending
- π Correlation IDs - Link related events together
- π Session Tracking - Automatic session ID generation
- π‘οΈ Type Safety - Full type hints and IDE autocomplete
pip install surfa-ingestfrom surfa_ingest import SurfaClient
# Initialize client with your ingest key
client = SurfaClient(ingest_key="sk_live_your_key_here")
# Track events
client.track({
"kind": "tool",
"subtype": "call_started",
"tool_name": "search_web",
"args": {"query": "AI news"}
})
client.track({
"kind": "tool",
"subtype": "call_completed",
"tool_name": "search_web",
"status": "success",
"latency_ms": 234
})
# Flush events to API
client.flush()Use the context manager to automatically track session lifecycle:
from surfa_ingest import SurfaClient
with SurfaClient(ingest_key="sk_live_your_key_here") as client:
# Session automatically started
client.track({
"kind": "tool",
"subtype": "call_started",
"tool_name": "search_web"
})
# Session automatically ended and events flushed on exitclient = SurfaClient(
ingest_key="sk_live_your_key_here",
api_url="https://api.surfa.dev", # Default: http://localhost:3000
flush_at=25, # Auto-flush after 25 events
timeout_s=10, # HTTP timeout in seconds
)Track which AI runtime is being used:
client = SurfaClient(ingest_key="sk_live_...")
client.set_runtime(
provider="anthropic",
model="claude-sonnet-4-5",
mode="messages"
)# Tool call started
client.track({
"kind": "tool",
"subtype": "call_started",
"tool_name": "search_web",
"direction": "request",
"args": {"query": "Python tutorials"}
})
# Tool call completed
client.track({
"kind": "tool",
"subtype": "call_completed",
"tool_name": "search_web",
"direction": "response",
"status": "success",
"latency_ms": 234,
"results": [{"title": "Learn Python", "url": "..."}]
})# Session started
client.session_started()
# Session ended
client.session_ended()# LLM request
client.track({
"kind": "runtime",
"subtype": "llm_request",
"direction": "outbound",
"messages": [{"role": "user", "content": "Hello"}],
"temperature": 0.7
})kind(str): Event type (e.g., "tool", "session", "runtime")
subtype(str): Event subtype (e.g., "call_started", "session_ended")tool_name(str): Name of the toolstatus(str): Status (e.g., "success", "error")direction(str): Direction (e.g., "request", "response")method(str): HTTP method or similarcorrelation_id(str): Correlation ID for pairing eventsspan_parent_id(str): Parent span ID for tracinglatency_ms(int): Latency in millisecondsts(str): Timestamp (ISO 8601 format, auto-generated if not provided)- Any additional fields will be included in the event payload
Events are automatically flushed when:
- Buffer reaches
flush_atevents (default: 25) - Context manager exits
flush()is called explicitly
from surfa_ingest import SurfaClient, SurfaConfigError, SurfaValidationError
try:
client = SurfaClient(ingest_key="invalid_key")
except SurfaConfigError as e:
print(f"Configuration error: {e}")
try:
client.track({"invalid": "event"}) # Missing 'kind'
except SurfaValidationError as e:
print(f"Validation error: {e}")The SDK uses Python's standard logging module:
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("surfa_ingest")Current Version: 0.1.0 (Alpha)
This SDK is in active development. The API may change in future versions.
- β Client initialization
- β Event buffering
- β Session management
- β Context manager support
- β Event validation
- β Runtime metadata
- β HTTP API integration
- β Automatic retry logic (3 retries with exponential backoff)
- π Background flushing
- π Async support
MIT
- π¦ PyPI: https://pypi.org/project/surfa-ingest/
- π Documentation: https://docs.surfa.dev
- π Issues: https://github.com/gamladz/surfa/issues
- π¬ Discussions: https://github.com/gamladz/surfa/discussions
Contributions are welcome! Please feel free to submit a Pull Request.
- Initial release
- Event buffering and batching
- Session management
- Context manager support
- Runtime metadata capture
- HTTP API integration with retry logic
- Event validation