Skip to main content

AI agent observability SDK with business metrics, ROI analytics, and 25+ trace format support

Project description

ThinkHive Python SDK

OpenTelemetry-based observability SDK for AI agents supporting 25 trace formats including LangSmith, Langfuse, Opik, Braintrust, Datadog, MLflow, and more.

Installation

pip install thinkhive

Quick Start

import thinkhive

# Initialize SDK (sets up both OTLP tracing and REST client)
thinkhive.init(
    api_key="your-api-key",  # or set THINKHIVE_API_KEY
    agent_id="my-bot",
    service_name="my-ai-agent"
)

# Trace LLM calls
@thinkhive.trace_llm(model_name="gpt-4", provider="openai")
def call_llm(prompt):
    response = openai.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )
    return response

# Trace retrieval operations
@thinkhive.trace_retrieval()
def search_documents(query):
    results = vector_db.search(query)
    return results

# Trace tool calls
@thinkhive.trace_tool(tool_name="web_search")
def search_web(query):
    return requests.get(f"https://api.example.com/search?q={query}")

HTTP-Based Trace Creation

For direct trace creation with evaluation control, use the traces API:

from thinkhive import traces

# Create trace with automatic evaluation
result = traces.create(
    agent_id="agent-123",
    user_message="What is the return policy?",
    agent_response="Items can be returned within 30 days.",
    outcome="success",
    run_evaluation=True  # Force evaluation on this trace
)

print(f"Trace ID: {result.id}")
if result.evaluation_queued:
    print("Evaluation will run asynchronously")

# Skip evaluation even if agent has auto_evaluate enabled
result = traces.create(
    agent_id="agent-123",
    user_message="Hello!",
    agent_response="Hi there!",
    run_evaluation=False
)

# Use agent's default auto_evaluate setting
result = traces.create(
    agent_id="agent-123",
    user_message="What are your hours?",
    agent_response="We are open 9 AM to 5 PM."
    # run_evaluation omitted - uses agent's setting
)

run_evaluation Parameter

The run_evaluation parameter controls whether traces are automatically evaluated:

Value Behavior
True Force evaluation on this trace
False Skip evaluation even if agent has auto_evaluate
None (default) Use agent's auto_evaluate setting

Environment Variables

  • THINKHIVE_API_KEY: Your ThinkHive API key
  • THINKHIVE_AGENT_ID: Your agent ID (alternative to API key)

Issues API (Clustered Failure Patterns)

The Issues API provides access to clustered failure patterns:

from thinkhive import issues

# List issues for an agent
all_issues = issues.list(agent_id="agent-123")

# Get a specific issue
issue = issues.get(issue_id="issue-456")

# Create a new issue
new_issue = issues.create(
    agent_id="agent-123",
    title="Refund policy confusion",
    type="hallucination",
    severity="high"
)

# Update an issue
issues.update(
    issue_id="issue-456",
    status="in_progress",
    assignee="developer@example.com"
)

# Get fixes for an issue
fixes = issues.get_fixes(issue_id="issue-456")

Analyzer API (User-Selected Analysis)

The Analyzer API provides user-selected trace analysis with cost estimation:

from thinkhive import analyzer

# Estimate cost before running analysis
estimate = analyzer.estimate_cost(
    trace_ids=["trace-1", "trace-2"],
    tier="standard"
)
print(f"Estimated cost: ${estimate['estimated_cost']}")

# Analyze specific traces
analysis = analyzer.analyze(
    trace_ids=["trace-1", "trace-2"],
    tier="standard",
    include_root_cause=True
)

# Get aggregated insights
summary = analyzer.summarize(
    agent_id="agent-123",
    start_date="2024-01-01",
    end_date="2024-01-31"
)

Business Metrics API

The Business Metrics API provides industry-driven metrics with historical tracking and external data support:

from thinkhive.api import business_metrics

# Get current metric value with status
metric = business_metrics.get_current("agent-123", metric_name="Deflection Rate")
print(f"{metric.metric_name}: {metric.value_formatted}")

if metric.status == "insufficient_data":
    needed = metric.min_trace_threshold - metric.trace_count
    print(f"Need {needed} more traces")

# Get historical data for graphing
history = business_metrics.get_history(
    "agent-123",
    "Deflection Rate",
    start_date="2024-01-01T00:00:00Z",
    end_date="2024-01-31T23:59:59Z",
    granularity="daily"
)

print(f"{len(history.data_points)} data points")
print(f"Change: {history.summary.change_percent}%")

# Record external metric values (from CRM, surveys, etc.)
result = business_metrics.record_value(
    "agent-123",
    metric_name="CSAT/NPS",
    value=4.5,
    period_start="2024-01-01T00:00:00Z",
    period_end="2024-01-07T23:59:59Z",
    unit="score",
    source="survey_system",
    source_details={"survey_id": "survey_456", "response_count": 150}
)
print(f"Recorded: {result.id}")

Metric Status Types

Status Description
ready Metric calculated and ready to display
insufficient_data Need more traces before calculation
awaiting_external External data source not connected
stale Data is older than expected

Helper Functions

from thinkhive import (
    is_metric_ready,
    needs_more_traces,
    awaiting_external_data,
    is_metric_stale,
    get_metric_status_message,
    format_metric_value
)

# Check metric status
if is_metric_ready(metric):
    print(f"Value: {metric.value_formatted}")
elif needs_more_traces(metric):
    print(get_metric_status_message(metric))

ROI Analytics API

from thinkhive.api import roi_analytics

# Get ROI summary
summary = roi_analytics.get_summary(
    start_date="2024-01-01T00:00:00Z",
    end_date="2024-01-31T23:59:59Z",
    agent_id="agent-123"
)
print(f"Revenue protected: ${summary.revenue_protected}")

# Get trends over time
trends = roi_analytics.get_trends(agent_id="agent-123")
for day in trends:
    print(f"{day.date}: {day.success_rate}% success")

# Calculate impact for specific data
impact = roi_analytics.calculate(
    user_message="Help me cancel my subscription",
    agent_response="I can help with that...",
    industry_config={"industry": "saas", "avg_customer_ltv": 10000}
)

API Reference

Module Description
traces Create and manage traces
issues Clustered failure patterns
analyzer User-selected trace analysis
business_metrics Industry-driven metrics with history
roi_analytics Business ROI and financial impact
runs Run-centric trace management
claims Facts vs inferences management
calibration Prediction accuracy tracking

License

MIT

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

thinkhive-4.0.0.tar.gz (42.2 kB view details)

Uploaded Source

Built Distribution

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

thinkhive-4.0.0-py3-none-any.whl (48.9 kB view details)

Uploaded Python 3

File details

Details for the file thinkhive-4.0.0.tar.gz.

File metadata

  • Download URL: thinkhive-4.0.0.tar.gz
  • Upload date:
  • Size: 42.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for thinkhive-4.0.0.tar.gz
Algorithm Hash digest
SHA256 ea9456d36ee7cd2673a3b5f76d169fb87f2bfb1a26de12ceceadb64164adb7d9
MD5 6cf52aed37b916cf94f62172b738cfca
BLAKE2b-256 c5b63135d23a99bfd8780808cf809a9489be78dc4aa9ceedbd40bb3a249b2e3a

See more details on using hashes here.

File details

Details for the file thinkhive-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: thinkhive-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 48.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for thinkhive-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5352e15edf1cf5822c2be691fbfac2c7131391172ee09525f836259971c02dfd
MD5 1aeaeef8c4ae623495480caf767996f7
BLAKE2b-256 7df2a01f0e4d3fac6ce8c61d307bb4268fbc33b19e0e188e97f81ea80faa2d35

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