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

# Strands Agents

> Learn how to integrate a Strands Agents project with Galileo using OpenTelemetry

Galileo supports logging traces from [Strands Agents SDK](https://strandsagents.com/) applications using OpenTelemetry.

## Set up OpenTelemetry

To log Strands Agents using Galileo, the first step is to set up OpenTelemetry.

<Steps>
  <Step title="Installation">
    Add the OpenTelemetry packages to your project:

    <CodeGroup>
      ```bash Terminal theme={null}
      pip install opentelemetry-api opentelemetry-sdk \
                  opentelemetry-exporter-otlp
      ```
    </CodeGroup>

    The `opentelemetry-api` and `opentelemetry-sdk` packages provide the core OpenTelemetry functionality. The `opentelemetry-exporter-otlp` package enables sending traces to Galileo's OTLP endpoint.
  </Step>

  <Step title="Create environment variables for your Galileo settings">
    Set environment variables for your Galileo settings, for example in a `.env` file.
    These environment variables are consumed by the `GalileoSpanProcessor`to authenticate
    and route traces to the correct Galileo Project and Log stream:

    {/*<!-- markdownlint-enable MD044 -->*/}

    <CodeGroup>
      ```ini .env theme={null}
      # Your Galileo API key
      GALILEO_API_KEY="your-galileo-api-key"

      # Your Galileo project name
      GALILEO_PROJECT="your-galileo-project-name"

      # The name of the Log stream you want to use for logging
      GALILEO_LOG_STREAM="your-galileo-log-stream "

      # Provide the console url below if you are using a
      # custom deployment, and not using the free tier, or app.galileo.ai.
      # This will look something like “console.galileo.yourcompany.com”.
      # GALILEO_CONSOLE_URL="your-galileo-console-url"
      ```
    </CodeGroup>
  </Step>

  <Step title="Self hosted deployments: Set the OTel endpoint">
    <Note>
      Skip this step if you are using Galileo Cloud.
    </Note>

    The OTel endpoint is different from Galileo's regular API endpoint and is specifically designed to receive telemetry data in the OTLP format.

    If you are using:

    * **Galileo Cloud** at [app.galileo.ai](https://app.galileo.ai), then you don't need to provide a custom OTel endpoint.
      The default endpoint `https://api.galileo.ai/otel/traces` will be used automatically.

    * A **self-hosted Galileo deployment**, replace the `https://api.galileo.ai/otel/traces` endpoint with your deployment URL. The format of this URL is based on your console URL, replacing `console` with `api` and appending `/otel/traces`.

    For example:

    * if your console URL is `https://console.galileo.example.com`, the OTel endpoint would be `https://api.galileo.example.com/otel/traces`
    * if your console URL is `https://console-galileo.apps.mycompany.com`, the OTel endpoint would be `https://api-galileo.apps.mycompany.com/otel/traces`

    The convention is to store this in the `GALILEO_CONSOLE_URL` environment variable. For example:

    <CodeGroup>
      ```python Python theme={null}
      os.environ["GALILEO_CONSOLE_URL"] = "https://api.galileo.ai"
      ```
    </CodeGroup>
  </Step>

  <Step title="Initialize and create the Galileo span processor">
    The `GalileoSpanProcessor` automatically configures authentication
    and metadata using your environment variables. It also:

    * Auto-builds OTLP headers using your Galileo credentials
    * Configures the correct OTLP trace endpoint
    * Registers a batch span processor that exports traces to Galileo

    <CodeGroup>
      ```python Python theme={null}
      from galileo import otel  

      # GalileoSpanProcessor (no manual OTLP config required) loads the env vars for 
      # the Galileo API key, Project, and Log stream. Make sure to set them first. 
      galileo_span_processor = otel.GalileoSpanProcessor(
          # Optional parameters if not set, uses env var
          # project=os.environ["GALILEO_PROJECT"], 
          # logstream=os.environ.get("GALILEO_LOG_STREAM"),  
      )
      ```
    </CodeGroup>
  </Step>
</Steps>

## Log a Strands ADK agent using OpenTelemetry

Once OpenTelemetry is configured, you can use the OTel capabilities of Strands to log traces.

<Steps>
  <Step title="Import the Strands Agent telemetry package">
    The Strands Agents SDK has telemetry support out of the box. Start by importing `StrandsTelemetry` with the following code:

    <CodeGroup>
      ```python Python theme={null}
      from strands.telemetry import StrandsTelemetry
      ```
    </CodeGroup>
  </Step>

  <Step title="Set up the exporter">
    You can now set up an OTel exporter for your Strands Agent:

    <CodeGroup>
      ```python Python theme={null}
      strands_telemetry = StrandsTelemetry()
      strands_telemetry.setup_otlp_exporter()
      ```
    </CodeGroup>

    When you run your Strands Agent code, traces will be logged to Galileo.
  </Step>
</Steps>

## Opt in to experimental OpenTelemetry semantic conventions

Starting in `strands-agents` v1.34.0, the Strands Agents SDK can emit generative AI traces using either the legacy OpenTelemetry semantic conventions or the newer experimental conventions:

* **Legacy mode** (default) — uses attributes such as `gen_ai.system` along with legacy event shapes.
* **Experimental mode** — follows the latest OpenTelemetry generative AI semantic conventions, using `gen_ai.provider.name` and structured `gen_ai.client.inference.operation.details` events that carry inputs, outputs, and system instructions.

Galileo automatically detects and maps both modes, so traces from either mode will appear in your Galileo Log stream without any changes to your Galileo setup.

If you want to opt in to the experimental conventions, set the following environment variable before your Strands Agent starts:

<CodeGroup>
  ```ini .env theme={null}
  OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental
  ```
</CodeGroup>

<Note>
  `OTEL_SEMCONV_STABILITY_OPT_IN` is read by the Strands Agents SDK at startup. Make sure it is exported in your process environment (or loaded from your `.env` file) before `StrandsTelemetry` is initialized.
</Note>

## Full example

Here is a full example based off the [Strands Agent quickstart](https://strandsagents.com/latest/documentation/docs/user-guide/quickstart/). You can find this project in the [Galileo SDK examples repo](https://github.com/rungalileo/sdk-examples/tree/main/python/agent/strands-agent).

To run this example, create a `.env` file with the following values set, or set them as environment variables:

```ini .env theme={null}
# AWS environment variables
AWS_BEARER_TOKEN_BEDROCK=your-aws-bedrock-token

# Galileo environment variables
GALILEO_API_ENDPOINT=your-galileo-otel-api-endpoint
GALILEO_API_KEY=your-galileo-api-key
GALILEO_PROJECT=your-galileo-project
GALILEO_LOG_STREAM=your-log-stream
```

Remember to update these to match your [Galileo API key](https://app.galileo.ai/settings/api-keys), AWS Bedrock token, [Galileo OTel endpoint](/sdk-api/third-party-integrations/opentelemetry-and-openinference#configure-the-otel-endpoint), project name, and Log stream name.

<CodeGroup>
  ```python Python theme={null}
  import os

  from strands import Agent, tool
  from strands.telemetry import StrandsTelemetry
  from strands_tools import calculator, current_time

  # Load environment variables from the .env file
  from dotenv import load_dotenv
  load_dotenv(override=True)

  # Export the Galileo OTel API endpoint for OTel
  # Export the Galileo OTel API endpoint for OTel
  os.environ["OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"] = os.environ.get(
      "GALILEO_API_ENDPOINT", 
      "https://api.galileo.ai/otel/traces"
  )

  # Export the Galileo OTel headers pointing to the correct API key,
  # # project, and log stream
  headers = {
      "Galileo-API-Key": os.environ["GALILEO_API_KEY"],
      "project": os.environ["GALILEO_PROJECT"],
      "logstream": os.environ["GALILEO_LOG_STREAM"],
  }

  os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = ",".join(
      [f"{k}={v}" for k, v in headers.items()]
  )

  # Setup telemetry for the Strands agent using Galileo as the OTel backend
  strands_telemetry = StrandsTelemetry()
  strands_telemetry.setup_otlp_exporter()

  # Define a custom tool as a Python function using the @tool decorator
  @tool
  def letter_counter(word: str, letter: str) -> int:
      """
      Count occurrences of a specific letter in a word.

      Args:
          word (str): The input word to search in
          letter (str): The specific letter to count

      Returns:
          int: The number of occurrences of the letter in the word
      """
      if not isinstance(word, str) or not isinstance(letter, str):
          return 0

      if len(letter) != 1:
          raise ValueError("The 'letter' parameter must be a single char")

      return word.lower().count(letter.lower())


  # Create an agent with tools from the community-driven strands-tools
  # package as well as our custom letter_counter tool
  agent = Agent(tools=[calculator, current_time, letter_counter])

  # Ask the agent a question that uses the available tools
  message = """
  I have 4 requests:

  1. What is the time right now?
  2. Calculate 3111696 / 74088
  3. Tell me how many letter R's are in the word "strawberry" 🍓
  """
  agent(message)
  ```
</CodeGroup>
