Connect Your Application

Connect Your Application to Arthur

This page covers everything you need to instrument a real application with Arthur. If you haven't yet deployed the engine and sent your first trace, start with the Quickstart →.

Install the SDK

Install arthur-observability-sdk with the extra for your LLM framework:

# OpenAI
pip install "arthur-observability-sdk[openai]"

# Anthropic
pip install "arthur-observability-sdk[anthropic]"

# LangChain
pip install "arthur-observability-sdk[langchain]"

# LiteLLM
pip install "arthur-observability-sdk[litellm]"

# Install all extras at once
pip install "arthur-observability-sdk[all]"
# Mastra users: install the Arthur exporter
npm install @mastra/arthur

Initialize Arthur

Create a single Arthur instance at application startup. You need your API key and task ID from the engine UI.

from arthur_observability_sdk import Arthur

arthur = Arthur(
    api_key="YOUR_API_KEY",      # or set ARTHUR_API_KEY env var
    base_url="YOUR_ENGINE_URL",  # or set ARTHUR_BASE_URL env var
    task_id="YOUR_TASK_ID",      # must be provided explicitly
)
import { ArthurExporter } from '@mastra/arthur';
import { Mastra } from '@mastra/core/mastra';

const mastra = new Mastra({
  observability: {
    configs: {
      arthur: {
        exporters: [
          new ArthurExporter({
            apiKey: process.env.ARTHUR_API_KEY,
            endpoint: process.env.ARTHUR_BASE_URL,
            taskId: process.env.ARTHUR_TASK_ID,
          }),
        ],
      },
    },
  },
});

Supported Frameworks

Call the relevant instrument_* method once after constructing Arthur. Every subsequent call to that framework is automatically captured as a trace.

arthur.instrument_openai()           # OpenAI
arthur.instrument_anthropic()        # Anthropic
arthur.instrument_langchain()        # LangChain
arthur.instrument_litellm()          # LiteLLM
arthur.instrument_crewai()           # CrewAI
arthur.instrument_openai_agents()    # OpenAI Agents SDK
arthur.instrument_llama_index()      # LlamaIndex
arthur.instrument_google_adk()       # Google ADK
arthur.instrument_bedrock()          # AWS Bedrock
arthur.instrument_mistralai()        # Mistral AI
arthur.instrument_groq()             # Groq
arthur.instrument_vertexai()         # Vertex AI
# ...and more — see [Integrations Overview →](doc:integrations-overview)

Each method requires the matching extra to be installed (e.g. pip install "arthur-observability-sdk[langchain]"). If the extra is missing, the method raises an ImportError with the install command.


Session and User Context

Tag spans with session and user identifiers to group traces by conversation or user in the Arthur UI.

As a context manager

with arthur.session("session-abc-123"):
    response = openai_client.chat.completions.create(...)

with arthur.user("user-42"):
    response = openai_client.chat.completions.create(...)

# Combine both
with arthur.attributes(session_id="session-abc-123", user_id="user-42"):
    response = openai_client.chat.completions.create(...)
// @mastra/arthur does not have a session/user context API.
// You can attach tags to a root span via tracingOptions:
const result = await agent.generate('Hello', {
  tracingOptions: { tags: ['production', 'experiment-v2'] }
});

As a decorator

@arthur.attributes(session_id="session-abc-123", user_id="user-42")
def handle_request(messages):
    return openai_client.chat.completions.create(messages=messages, ...)

arthur.attributes() accepts the full set of OpenInference context attributes: session_id, user_id, metadata, tags, prompt_template, prompt_template_version, prompt_template_variables.


Shutdown

Call arthur.shutdown() before your process exits to flush all pending traces. Without this, spans buffered in the BatchSpanProcessor may be lost.

arthur.shutdown()

For long-running servers, hook into your framework's shutdown lifecycle:

import atexit
atexit.register(arthur.shutdown)
// Flush without shutting down:
await exporter.flush();

// Flush and terminate:
await exporter.shutdown();

Next Steps

What you want to doWhere to go
Score traces automatically with LLM judgesLLM Evaluators →
Run evaluations against production trafficRun Evaluations →
Get notified when scores dropAlerts →
Version and manage your promptsPrompts →
Add real-time guardrailsGuardrails →