Watsonx (tracing)

IBM watsonx.ai is IBM’s enterprise AI platform for foundation models and embeddings. Respan traces the official ibm-watsonx-ai Python SDK so direct Watsonx calls show up with prompts, completions, tool calls, embeddings, token usage, latency, workflow names, and custom metadata.

Use this page when your application calls Watsonx directly with IBM credentials.

Create an account at platform.respan.ai and grab an API key.

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

See Watsonx gateway setup to route Watsonx calls through the Respan gateway.

Setup

1

Install packages

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

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
$export WATSONX_API_KEY="YOUR_WATSONX_API_KEY"
$export WATSONX_PROJECT_ID="YOUR_WATSONX_PROJECT_ID"
$export WATSONX_URL="https://us-south.ml.cloud.ibm.com"

RESPAN_API_KEY exports traces to Respan. WATSONX_API_KEY, WATSONX_PROJECT_ID, and WATSONX_URL are used by the IBM Watsonx SDK.

3

Initialize and run

1import os
2
3from ibm_watsonx_ai import Credentials
4from ibm_watsonx_ai.foundation_models import ModelInference
5from respan import Respan
6from respan_instrumentation_watsonx import WatsonxInstrumentor
7
8respan = Respan(instrumentations=[WatsonxInstrumentor()])
9
10credentials = Credentials(
11 api_key=os.environ["WATSONX_API_KEY"],
12 url=os.getenv("WATSONX_URL", "https://us-south.ml.cloud.ibm.com"),
13)
14
15model = ModelInference(
16 model_id=os.getenv("WATSONX_MODEL_ID", "ibm/granite-3-8b-instruct"),
17 credentials=credentials,
18 project_id=os.environ["WATSONX_PROJECT_ID"],
19 validate=False,
20)
21
22response = model.generate(
23 prompt="Say hello in three languages.",
24 params={"max_new_tokens": 60},
25)
26print(response["results"][0]["generated_text"])
27
28respan.flush()
4

View your trace

Open the Traces page to see the Watsonx SDK spans with model names, prompt and completion content, token usage, latency, and metadata.

Supported calls

WatsonxInstrumentor() traces these official ibm-watsonx-ai SDK methods:

ClassMethods
ModelInferencegenerate, generate_text, generate_text_stream, chat, chat_stream, agenerate, agenerate_stream, achat, achat_stream
Embeddingsgenerate, embed_query, embed_documents, agenerate, aembed_query, aembed_documents

Chat spans include tool definitions from llm.request.functions and tool calls from the assistant completion. Embedding spans include inputs and usage metadata but do not export embedding vectors.

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, for example WatsonxInstrumentor().
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag, for example "production".

Attributes

In Respan()

Set defaults at initialization. These apply to all spans.

1from respan import Respan
2from respan_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 environment="production",
9)

With propagate_attributes

Override attributes for a single request or workflow.

1from respan import Respan, propagate_attributes
2from respan_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 response = model.generate(prompt=prompt, params={"max_new_tokens": 60})
13 print(response["results"][0]["generated_text"])
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.

Decorators (optional)

Decorators are not required. Watsonx SDK calls are auto-traced. Use @workflow and @task when you want to group related Watsonx calls into a named trace.

1from respan import Respan, workflow
2from respan_instrumentation_watsonx import WatsonxInstrumentor
3
4respan = Respan(instrumentations=[WatsonxInstrumentor()])
5
6@workflow(name="watsonx_answer")
7def answer(question: str):
8 response = model.chat(
9 messages=[{"role": "user", "content": question}],
10 params={"max_new_tokens": 80},
11 )
12 return response["choices"][0]["message"]["content"]
13
14print(answer("What does tracing capture?"))
15respan.flush()