IBM Watsonx

IBM Watsonx is IBM’s enterprise AI platform that provides foundation models, generative AI, and machine learning tools. It offers models like Granite and supports text generation, summarization, and classification for enterprise workloads. Respan gives you full observability over every Watsonx call, prompt, and decoding step — and gateway routing through the OpenAI-compatible Respan endpoint.

Create an account at platform.respan.ai and grab an API key. For gateway, also add credits or a provider key.

Run npx @respan/cli setup to set up with your coding agent.

Setup

1

Install packages

$pip install respan-ai opentelemetry-instrumentation-watsonx ibm-watsonx-ai
2

Set environment variables

$export WATSONX_API_KEY="YOUR_WATSONX_API_KEY"
$export WATSONX_PROJECT_ID="YOUR_WATSONX_PROJECT_ID"
$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

WATSONX_* are used for Watsonx requests. RESPAN_API_KEY is used to export traces to Respan.

3

Initialize and run

1import os
2from ibm_watsonx_ai.foundation_models import Model
3from ibm_watsonx_ai.metanames import GenTextParamsMetaNames
4from respan import Respan
5from opentelemetry.instrumentation.watsonx import WatsonxInstrumentor
6
7respan = Respan(instrumentations=[WatsonxInstrumentor()])
8
9params = {
10 GenTextParamsMetaNames.MAX_NEW_TOKENS: 100,
11 GenTextParamsMetaNames.TEMPERATURE: 0.7,
12}
13
14model = Model(
15 model_id="ibm/granite-13b-instruct-v2",
16 credentials={
17 "apikey": os.environ["WATSONX_API_KEY"],
18 "url": "https://us-south.ml.cloud.ibm.com",
19 },
20 project_id=os.environ["WATSONX_PROJECT_ID"],
21 params=params,
22)
23
24response = model.generate_text("Say hello in three languages.")
25print(response)
26respan.flush()
4

View your trace

Open the Traces page to see your auto-instrumented Watsonx spans with prompts, tokens, and decoding parameters.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate (e.g. WatsonxInstrumentor()).
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

Set defaults at initialization — these apply to all spans.

1from respan import Respan
2from opentelemetry.instrumentation.watsonx import WatsonxInstrumentor
3
4respan = Respan(
5 instrumentations=[WatsonxInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "watsonx-api", "version": "1.0.0"},
8)

With propagate_attributes

Override per-request using a context scope.

1from respan import Respan, propagate_attributes
2from opentelemetry.instrumentation.watsonx import WatsonxInstrumentor
3
4respan = Respan(instrumentations=[WatsonxInstrumentor()])
5
6def handle_request(user_id: str, prompt: str):
7 with propagate_attributes(
8 customer_identifier=user_id,
9 thread_identifier="conv_abc_123",
10 metadata={"plan": "pro"},
11 ):
12 print(model.generate_text(prompt))
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.