Quickstart

Quickstart

Get Mantis SDK tracing working in 5 minutes.

1. Install the SDK

pip install mantisdk python-dotenv

2. Set Environment Variables

Create a .env file:

# Insight credentials
INSIGHT_HOST=https://insight.withmetis.ai
INSIGHT_PUBLIC_KEY=pk-lf-your-public-key
INSIGHT_SECRET_KEY=sk-lf-your-secret-key
 
# Your LLM API key (example: OpenAI)
OPENAI_API_KEY=sk-your-openai-key

3. Initialize Tracing

from dotenv import load_dotenv
load_dotenv()
 
import mantisdk.tracing as tracing
 
# Auto-detect credentials from environment
tracing.init()

4. Add Tracing to Your Code

Option A: Decorator (Recommended)

Use @tracing.trace to automatically capture function inputs and outputs:

import mantisdk.tracing as tracing
 
@tracing.trace
def answer_question(question: str) -> str:
    # Your LLM logic here
    response = call_llm(question)
    return response
 
# Usage
result = answer_question("What is the capital of France?")

Option B: Context Manager

Use tracing.trace() as a context manager for explicit control:

import mantisdk.tracing as tracing
 
def process_request(query: str) -> dict:
    with tracing.trace("process_request", input=query) as span:
        # Add custom attributes
        span.set_attribute(tracing.semconv.USER_ID, "user-123")
 
        # Your logic
        result = analyze(query)
 
        # Set output
        span.set_attribute(tracing.semconv.TRACE_OUTPUT, str(result))
 
        return result

5. Nested Spans

Create child spans for sub-operations:

import mantisdk.tracing as tracing
 
def my_agent(query: str) -> str:
    with tracing.trace("agent_workflow", input=query) as root:
        # Step 1: Classify
        with tracing.span("classify_query") as s1:
            query_type = classify(query)
            s1.set_attribute("query_type", query_type)
 
        # Step 2: Process
        with tracing.span("process_query") as s2:
            result = process(query, query_type)
 
        # Step 3: Generate response
        with tracing.span("generate_response") as s3:
            response = generate(result)
            s3.set_attribute(tracing.semconv.OBSERVATION_OUTPUT, response)
 
        root.set_attribute(tracing.semconv.TRACE_OUTPUT, response)
        return response

6. Tool Calls

Trace tool executions with tracing.tool():

import mantisdk.tracing as tracing
 
def search_database(query: str) -> list:
    with tracing.tool("search_database", input=query) as span:
        results = db.search(query)
        span.set_attribute(tracing.semconv.OBSERVATION_OUTPUT, str(len(results)))
        return results

7. Clean Shutdown

Always call shutdown() before your program exits:

import atexit
import mantisdk.tracing as tracing
 
tracing.init()
 
# Register shutdown handler
atexit.register(tracing.shutdown)
 
# Or manually at the end
try:
    main()
finally:
    tracing.shutdown()

Complete Example

#!/usr/bin/env python3
"""Simple agent with Mantis SDK tracing."""
 
from dotenv import load_dotenv
load_dotenv()
 
import mantisdk.tracing as tracing
 
# Initialize tracing
tracing.init(service_name="my-agent")
 
@tracing.trace
def classify_query(query: str) -> str:
    """Classify the user query."""
    if "weather" in query.lower():
        return "weather"
    return "general"
 
@tracing.trace
def answer_query(query: str, query_type: str) -> str:
    """Generate an answer based on query type."""
    if query_type == "weather":
        return "I can help with weather! Which city?"
    return "I can help with that question."
 
def main():
    query = "What's the weather like?"
 
    with tracing.trace("agent_session", input=query) as span:
        span.set_attribute(tracing.semconv.USER_ID, "demo-user")
        span.set_attribute(tracing.semconv.SESSION_ID, "session-001")
 
        # Process query
        query_type = classify_query(query)
        response = answer_query(query, query_type)
 
        span.set_attribute(tracing.semconv.TRACE_OUTPUT, response)
        print(response)
 
if __name__ == "__main__":
    try:
        main()
    finally:
        tracing.shutdown()

Ingest external agent outputs

If you already have externally produced trace/score results and want to write them directly to Insight, use the SDK ingestion helper: Insight Ingestion (SDK).

Next Steps