> ## Documentation Index
> Fetch the complete documentation index at: https://futureagi.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Implementing Tracing

> We recommend starting with the auto-instrumentation first. For advanced customization and granular control, you can directly utilize our OTEL-compliant instrumentation API.

## 1. Tracing Integrations: Quick Start (Auto-Instrumentation)

Implement trace logging effortlessly with our pre-built tracing integrations. These integrations offer flexibility for further customization as needed.

| LLM Models                                                                                 | Orchestration Frameworks                                                                             | Other                                                                                          |
| ------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
| [OpenAI](/future-agi/products/observability/auto-instrumentation/openai)                   | [LlamaIndex](/future-agi/products/observability/auto-instrumentation/llamaindex)                     | [DSPY](/future-agi/products/observability/auto-instrumentation/dspy)                           |
| [OpenAI Agents SDK](/future-agi/products/observability/auto-instrumentation/openai_agents) | [LlamaIndex Workflows](/future-agi/products/observability/auto-instrumentation/llamaindex-workflows) | [Guardrails AI](/future-agi/products/observability/auto-instrumentation/guardrails)            |
| [Vertex AI (Gemini)](/future-agi/products/observability/auto-instrumentation/vertexai)     | [Langchain](/future-agi/products/observability/auto-instrumentation/langchain)                       | [Hugging Face smolagents](/future-agi/products/observability/auto-instrumentation/smol_agents) |
| [AWS Bedrock](/future-agi/products/observability/auto-instrumentation/bedrock)             | [LangGraph](/future-agi/products/observability/auto-instrumentation/langgraph)                       | [Ollama](/future-agi/products/observability/auto-instrumentation/ollama)                       |
| [Mistral AI](/future-agi/products/observability/auto-instrumentation/mistralai)            | [LiteLLM](/future-agi/products/observability/auto-instrumentation/litellm)                           | [Instructor](/future-agi/products/observability/auto-instrumentation/instructor)               |
| [Anthropic](/future-agi/products/observability/auto-instrumentation/anthropic)             | [CrewAI](/future-agi/products/observability/auto-instrumentation/crewai)                             |                                                                                                |
| [Groq](/future-agi/products/observability/auto-instrumentation/groq)                       | [Haystack](/future-agi/products/observability/auto-instrumentation/haystack)                         |                                                                                                |
| [Together AI](/future-agi/products/observability/auto-instrumentation/togetherai)          | [Autogen](/future-agi/products/observability/auto-instrumentation/autogen)                           |                                                                                                |
|                                                                                            | [PromptFlow](/future-agi/products/observability/auto-instrumentation/promptflow)                     |                                                                                                |

## 2. Manual Instrumentation

For applications requiring precise control over trace data, Future AGI provides OpenTelemetry (OTEL) support. This enables custom span creation and modification using the OpenTelemetry Trace API.

## Implementation Guide

### Step 1: System Requirements

<CodeGroup>
  ```bash Python theme={null}
  pip install fi-instrumentation-otel
  pip install traceAI-openai
  ```

  ```bash JS/TS theme={null}
  npm install @traceai/openai
  ```
</CodeGroup>

Prerequisites:

* **Python**: version 3.9 to 3.12, [Future AGI Instrumentation Package](https://pypi.org/project/fi-instrumentation-otel/)
* **JavaScript**: CommonJS/TS Module System, Node.JS/TS version 18.x or higher, [Future AGI Instrumentation Package](https://www.npmJS/TS.com/package/@traceai/fi-core)

### Step 2: Set Environment Variables

<CodeGroup>
  ```python Python theme={null}
  import os
  os.environ["FI_API_KEY"] = "your-futureagi-api-key"
  os.environ["FI_SECRET_KEY"] = "your-futureagi-secret-key"
  ```

  ```javascript JS/TS theme={null}
  process.env.FI_API_KEY = FI_API_KEY;
  process.env.FI_SECRET_KEY = FI_SECRET_KEY;
  ```
</CodeGroup>

### Step 3: Configuring a Tracer

Setting up an OTEL tracer typically requires complex boilerplate code. Future AGI simplifies this process with our register helper function:

<CodeGroup>
  ```python Python theme={null}
  from traceai_openai import OpenAIInstrumentor
  from fi_instrumentation import register

  # Initialize OTel using our register function
  trace_provider = register(
      project_type=ProjectType.EXPERIMENT,
      project_name="FUTURE_AGI",
      project_version_name="openai-exp",
  )
  ```

  ```javascript JS/TS theme={null}
  const { register, ProjectType } = require("@traceai/fi-core");

  const traceProvider = register({
      project_type: ProjectType.OBSERVE,
      project_name: "FUTURE_AGI"
  });
  ```
</CodeGroup>

### Step 4: Span Implementation

When using our Auto-Instrumenters, span creation is handled automatically. You can further customize these spans as needed.

<CodeGroup>
  ```python Python theme={null}
  OpenAIInstrumentor().instrument(tracer_provider=trace_provider)
  ```

  ```javascript JS/TS theme={null}
  const { OpenAIInstrumentation } = require("@traceai/openai");

  const openaiInstrumentation = new OpenAIInstrumentation({});

  registerInstrumentations({
      instrumentations: [openaiInstrumentation],
      tracerProvider: tracerProvider,
  });
  ```
</CodeGroup>

For complete control over spans you can use manual instrumentation:

<CodeGroup>
  ```python Python theme={null}
  from opentelemetry import trace

  trace.set_tracer_provider(trace_provider)
  tracer = trace.get_tracer(__name__)
  ```

  ```javascript JS/TS theme={null}
  const { trace, context } = require("@opentelemetry/api");
  const { AsyncLocalStorageContextManager } = require("@opentelemetry/context-async-hooks");
  const { register } = require("@traceai/fi-core");
  const { ProjectType } = require("@traceai/fi-core");
  const { registerInstrumentations } = require("@opentelemetry/instrumentation");

  // Activate a context manager for consistent context propagation
  context.setGlobalContextManager(new AsyncLocalStorageContextManager());

  // Initialize and get a tracer using our register function
  const traceProvider = register({
      project_type: ProjectType.OBSERVE,
      project_name: "FUTURE_AGI"
  });

  const tracer = traceProvider.getTracer("manual-instrumentation-example");
  ```
</CodeGroup>

Next we create spans by starting spans and defining our name and other attributes:

<CodeGroup>
  ```python Python theme={null}
  def process_operation():
      with tracer.start_as_current_span("span-name") as span:
          # Execute operations tracked by 'span'
          print("doing some work...")
          # When the 'with' block goes out of scope, 'span' is automatically closed
  ```

  ```javascript JS/TS theme={null}
  function processOperation() {
      const q1 = () => tracer.startActiveSpan('processOperation', (span) => {
          span.setAttribute('operation', 'processOperation');
          span.end();
      });

      const q2 = () => tracer.startActiveSpan('processChildOperation', (span) => {
          span.setAttribute('operation', 'processChildOperation');
          span.end();
      });

      q1();
      q2();
  }
  ```
</CodeGroup>

You can also use start\_span to create a span without making it the current span. This is usually done to track concurrent or asynchronous operations.

#### Implementing Nested Spans

Track sub-operations within larger operations by creating hierarchical span relationships:

<CodeGroup>
  ```python Python theme={null}
  def process_operation():
      with tracer.start_as_current_span("parent") as parent:
          # Execute parent-level operations
          print("doing some work...")
          # Create nested span for sub-operations
          with tracer.start_as_current_span("child") as child:
              # Execute child-level operations
              print("doing some nested work...")
              # Child span closes automatically when it's out of scope
  ```

  ```javascript JS/TS theme={null}
  // Implementation would be similar with nested startActiveSpan calls
  function processOperation() {
      tracer.startActiveSpan("parent", (parentSpan) => {
          // Execute parent-level operations
          console.log("doing some work...");
          
          tracer.startActiveSpan("child", (childSpan) => {
              // Execute child-level operations
              console.log("doing some nested work...");
              childSpan.end();
          });
          
          parentSpan.end();
      });
  }
  ```
</CodeGroup>

In our platform `child` span appears as a nested component under the `parent` span.

#### Decorator Implementation

<CodeGroup>
  ```python Python theme={null}
  @tracer.start_as_current_span("process_operation")
  def process_operation():
      print("doing some work...")
  ```

  ```javascript JS/TS theme={null}
  // JavaScript doesn't have decorators in the same way, but you can achieve similar functionality
  const decoratedFunction = (fn) => {
      return (...args) => {
          return tracer.startActiveSpan("process_operation", (span) => {
              try {
                  const result = fn(...args);
                  span.end();
                  return result;
              } catch (error) {
                  span.recordException(error);
                  span.end();
                  throw error;
              }
          });
      };
  };

  const processOperation = decoratedFunction(() => {
      console.log("doing some work...");
  });
  ```
</CodeGroup>

Use of the decorator is equivalent to creating the span inside `process_operation()` and ending it when `process_operation()` is finished.

To use the decorator, you must have a tracer instance in scope for your function declaration.
