Observability for TrueFoundry with Opik

TrueFoundry AI Gateway is the proxy layer that sits between your applications and the LLM providers and MCP Servers. It is an enterprise-grade platform that enables users to access 1000+ LLMs using a unified interface while taking care of observability and governance.

The gateway exports OpenTelemetry traces, and Opik ingests them at its native OTLP endpoint. You get one trace for every request that passes through the gateway, without a change to your application code.

Gateway Overview

The TrueFoundry AI Gateway gives you these features:

  • Unified OpenAI-compatible endpoint: One endpoint routes to any supported model, from a commercial provider or from your own deployment.
  • Native MCP support: Connect enterprise tools to your agents, and apply OAuth2, RBAC, and metadata policies to each tool call.
  • Routing and reliability: Load balancing by weight, latency, or priority, with automatic fallback chains and retries.
  • Governance controls: Rate limits, quotas per user and per team, budget alerts, spend caps, and scoped API keys with RBAC.
  • Observability: Token, latency, cost, and error metrics for each request, with OpenTelemetry export to a platform such as Opik.
  • Data sovereignty: VPC and on-premises deployment options for compliance and data privacy.

To learn more about the gateway, see the TrueFoundry AI Gateway documentation.

Account Setup

Comet provides a hosted version of the Opik platform. Simply create an account and grab your API Key.

You can also run the Opik platform locally, see the installation guide for more information.

Integration Options

You can connect TrueFoundry to Opik in two ways. The two options work together, so you can also use both.

OptionHow it worksUse it when
Gateway trace exportYou enable the OpenTelemetry exporter in the gateway settings. The gateway sends one trace per request.You want all traffic from all teams, and you do not want to change application code.
Application tracingYou wrap the OpenAI client in your code with the Opik SDK.You want to group LLM calls into multi-step traces, and to add tags, metadata, and feedback scores.

Gateway Trace Export

The gateway exports OpenTelemetry traces over OTLP. Opik accepts these traces at its native OTLP endpoint. TrueFoundry also documents this setup, with a screenshot of each field, on the Comet Opik page in the TrueFoundry documentation.

Prerequisites

  • A TrueFoundry account with access to the AI Gateway. See the TrueFoundry quick start.
  • An Opik account, on Opik Cloud or on your own deployment.
  • Your Opik API key, your Opik workspace name, and the name of the Opik project for the traces.

Opik ingests traces only. Keep the Otel Metrics Exporter Configuration toggle disabled. Opik also accepts HTTP transport only, so do not select the gRPC configuration.

Trace Endpoint

The Opik OTLP base endpoint is /api/v1/private/otel. TrueFoundry sends traces to the signal-specific endpoint, which is the base endpoint with /v1/traces at the end. TrueFoundry does not add this path for you, so enter the full path in the Endpoint field.

https://www.comet.com/opik/api/v1/private/otel/v1/traces

Configuring the Exporter

1

Get your Opik API key, workspace, and project

Open your Opik account settings and copy your API key. Note the workspace name from the workspace switcher. Then choose the name of the project for the traces. Opik creates the project on the first trace if it does not exist.

2

Open the OTEL configuration in TrueFoundry

In the TrueFoundry dashboard, go to AI GatewayControlsSettings. Find the OTEL Config section and click the edit button.

3

Enable the traces exporter

Turn on the Otel Traces Exporter Configuration toggle, then enter the values below.

FieldValue
ProtocolHTTP Configuration
EndpointThe trace endpoint for your deployment, from the section above
EncodingProto
Header AuthorizationYour Opik API key
Header Comet-WorkspaceYour Opik workspace name
Header projectNameThe name of the Opik project for the traces

The Authorization header value is the raw API key. Do not add the Bearer prefix.

4

Save the configuration

Leave the Otel Metrics Exporter Configuration toggle disabled, then click Save.

5

Verify the integration

Send a request through the gateway. Then open the traces view for your project in Opik. You see spans from tfy-llm-gateway, with the inputs, the outputs, the token counts, the latency, and the cost.

Configuration Reference

ConfigurationValue
Traces endpoint<opik-host>/api/v1/private/otel/v1/traces
Metrics endpointNot supported. Opik ingests traces only.
ProtocolHTTP. Opik does not accept gRPC.
EncodingProto. Opik also accepts JSON.
AuthorizationYour Opik API key, raw, with no Bearer prefix
Comet-WorkspaceYour Opik workspace name
projectNameThe target Opik project. Opik uses Default Project when you omit this header.

For more information about the Opik OTLP endpoint and its headers, see the OpenTelemetry integration guide.

Troubleshooting

Check that the Endpoint field ends with /v1/traces. TrueFoundry does not add this path for you, so the base OTLP endpoint on its own returns an error. Check also that the protocol is HTTP Configuration and that the encoding is Proto.

The Authorization header takes the raw Opik API key. Remove the Bearer prefix if you added one. Then check that the Comet-Workspace header matches your Opik workspace name exactly.

The projectName header sets the project. Opik uses the project named Default Project when this header is absent or empty. Add the header, then send a new request.

This is expected. Opik ingests traces only, and it rejects OTLP metrics. Keep the Otel Metrics Exporter Configuration toggle disabled.

Application Tracing

The gateway exposes an OpenAI-compatible API, so you can use the Opik OpenAI SDK wrapper to log gateway calls as generations in Opik. Use this option when you want to group several LLM calls into one trace.

Installation

First, ensure you have both opik and openai packages installed:

$pip install opik openai

Configuring Opik

Configure the Opik Python SDK for your deployment type. See the Python SDK Configuration guide for detailed instructions on:

  • CLI configuration: opik configure
  • Code configuration: opik.configure()
  • Self-hosted vs Cloud vs Enterprise setup
  • Configuration files and environment variables

Configuring TrueFoundry

You need a TrueFoundry API key and the base URL of your gateway.

  • Base URL: https://gateway.truefoundry.ai for TrueFoundry SaaS. For a self-hosted gateway, get the base URL from the Code Snippet tab of the TrueFoundry playground.
  • API key: Create a Personal Access Token or a Virtual Account Token in the Access section of the TrueFoundry platform. See Generating TrueFoundry API keys.

Set your configuration as environment variables:

$export TRUEFOUNDRY_API_KEY="<your-truefoundry-api-key>"
$export TRUEFOUNDRY_BASE_URL="<your-truefoundry-base-url>"

Or set them programmatically:

1import os
2import getpass
3
4if "TRUEFOUNDRY_API_KEY" not in os.environ:
5 os.environ["TRUEFOUNDRY_API_KEY"] = getpass.getpass("Enter your TrueFoundry API key: ")
6
7if "TRUEFOUNDRY_BASE_URL" not in os.environ:
8 os.environ["TRUEFOUNDRY_BASE_URL"] = input("Enter your TrueFoundry base URL: ")

Simple LLM Call

1import os
2from opik.integrations.openai import track_openai
3from openai import OpenAI
4
5# Create an OpenAI client with TrueFoundry's base URL
6client = OpenAI(
7 api_key=os.environ["TRUEFOUNDRY_API_KEY"],
8 base_url=os.environ["TRUEFOUNDRY_BASE_URL"]
9)
10
11# Wrap the client with Opik tracking
12client = track_openai(client, project_name="truefoundry-integration-demo")
13
14# Make a chat completion request
15response = client.chat.completions.create(
16 model="openai-main/gpt-4o",
17 messages=[
18 {"role": "system", "content": "You are a knowledgeable AI assistant."},
19 {"role": "user", "content": "What is the largest city in France?"}
20 ]
21)
22
23# Print the assistant's reply
24print(response.choices[0].message.content)

The model value is the TrueFoundry model ID, in the format provider_account/model_name. Copy it from the Code Snippet tab of the TrueFoundry playground. See the TrueFoundry Chat Completions API for the full request format.

Multi-Step Traces

If you have multiple steps in your LLM pipeline, you can use the @track decorator to log the traces for each step. If TrueFoundry is called within one of these steps, the LLM call will be associated with that corresponding step:

1import os
2from opik import track
3from opik.integrations.openai import track_openai
4from openai import OpenAI
5
6# Create and wrap the OpenAI client with TrueFoundry's base URL
7client = OpenAI(
8 api_key=os.environ["TRUEFOUNDRY_API_KEY"],
9 base_url=os.environ["TRUEFOUNDRY_BASE_URL"]
10)
11client = track_openai(client)
12
13@track
14def generate_response(prompt: str):
15 response = client.chat.completions.create(
16 model="openai-main/gpt-4o",
17 messages=[
18 {"role": "system", "content": "You are a knowledgeable AI assistant."},
19 {"role": "user", "content": prompt}
20 ]
21 )
22 return response.choices[0].message.content
23
24@track
25def refine_response(initial_response: str):
26 response = client.chat.completions.create(
27 model="openai-main/gpt-4o",
28 messages=[
29 {"role": "system", "content": "You enhance and polish text responses."},
30 {"role": "user", "content": f"Please improve this response: {initial_response}"}
31 ]
32 )
33 return response.choices[0].message.content
34
35@track(project_name="truefoundry-integration-demo")
36def generate_and_refine(prompt: str):
37 # First LLM call: Generate initial response
38 initial = generate_response(prompt)
39
40 # Second LLM call: Refine the response
41 refined = refine_response(initial)
42
43 return refined
44
45# Example usage
46result = generate_and_refine("Explain quantum computing in simple terms.")

The trace will show nested LLM calls with hierarchical spans.

TrueFoundry Documentation

Next steps

Further Improvements

If you have suggestions for improving the TrueFoundry integration, please let us know by opening an issue on GitHub.