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

# Watsonx AI

> Trace IBM Watsonx AI model calls with Netra auto-instrumentation. Monitor foundation model prompts, completions, token usage, and latency on IBM Cloud.

<img src="https://mintcdn.com/netra/IXT7TOAHn4HQhvyF/images/integration-logos/llm-providers/watson-x.png?fit=max&auto=format&n=IXT7TOAHn4HQhvyF&q=85&s=337cf82aea7e4261cb11d45035c7d512" alt="IBM watsonx" width="466" height="80" data-path="images/integration-logos/llm-providers/watson-x.png" />

## Installation

Install both the Netra SDK and IBM Watsonx SDK:

<CodeGroup>
  ```bash Python theme={null}
  pip install netra-sdk ibm-watsonx-ai
  ```

  ```bash Typescript theme={null}
  npm install netra-sdk @ibm-cloud/watsonx-ai
  ```
</CodeGroup>

## Usage

Initialize the Netra SDK with Watsonx instrumentation enabled. The SDK automatically traces all Watsonx API calls once initialized.

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  from ibm_watsonx_ai.foundation_models import Model
  from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
  import os

  # Initialize Netra with Watsonx instrumentation
  Netra.init(
      app_name="my-ai-app",
      headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
      trace_content=True
  )

  # Use Watsonx client as usual - all calls are automatically traced
  model = Model(
      model_id="ibm/granite-13b-chat-v2",
      credentials={
          "url": "https://us-south.ml.cloud.ibm.com",
          "apikey": os.environ.get("WATSONX_API_KEY")
      },
      project_id=os.environ.get("WATSONX_PROJECT_ID")
  )

  response = model.generate_text(prompt="What is observability?")
  print(response)
  ```

  ```typescript Typescript theme={null}
  import { Netra } from "netra-sdk";
  import WatsonxAI from "@ibm-cloud/watsonx-ai";

  // Initialize Netra with Watsonx instrumentation (must await)
  await Netra.init({
    appName: "my-ai-app",
    headers: `x-api-key=${process.env.NETRA_API_KEY}`,
    traceContent: true
  });

  // Use Watsonx client as usual - all calls are automatically traced
  const watsonx = new WatsonxAI({
    version: "2024-05-31",
    serviceUrl: "https://us-south.ml.cloud.ibm.com",
    apikey: process.env.WATSONX_API_KEY
  });

  async function main() {
    const response = await watsonx.generateText({
      input: "What is observability?",
      modelId: "ibm/granite-13b-chat-v2",
      projectId: process.env.WATSONX_PROJECT_ID
    });

    console.log(response.results[0].generated_text);
  }

  main();
  ```
</CodeGroup>

### Streaming Responses

The SDK automatically handles streaming responses and captures the complete output:

<CodeGroup>
  ```python Python theme={null}
  model = Model(
      model_id="ibm/granite-13b-chat-v2",
      credentials={
          "url": "https://us-south.ml.cloud.ibm.com",
          "apikey": os.environ.get("WATSONX_API_KEY")
      },
      project_id=os.environ.get("WATSONX_PROJECT_ID")
  )

  for chunk in model.generate_text_stream(prompt="Tell me a story"):
      print(chunk, end="")
  ```

  ```typescript Typescript theme={null}
  const stream = await watsonx.generateTextStream({
    input: "Tell me a story",
    modelId: "ibm/granite-13b-chat-v2",
    projectId: process.env.WATSONX_PROJECT_ID
  });

  for await (const chunk of stream) {
    if (chunk.results && chunk.results[0]) {
      process.stdout.write(chunk.results[0].generated_text);
    }
  }
  ```
</CodeGroup>

### Advanced Parameters

Configure generation parameters for more control:

<CodeGroup>
  ```python Python theme={null}
  generate_params = {
      GenParams.MAX_NEW_TOKENS: 100,
      GenParams.TEMPERATURE: 0.7,
      GenParams.TOP_P: 0.9
  }

  model = Model(
      model_id="ibm/granite-13b-chat-v2",
      credentials={
          "url": "https://us-south.ml.cloud.ibm.com",
          "apikey": os.environ.get("WATSONX_API_KEY")
      },
      project_id=os.environ.get("WATSONX_PROJECT_ID"),
      params=generate_params
  )

  response = model.generate_text(prompt="Explain AI")
  print(response)
  ```

  ```typescript Typescript theme={null}
  const response = await watsonx.generateText({
    input: "Explain AI",
    modelId: "ibm/granite-13b-chat-v2",
    projectId: process.env.WATSONX_PROJECT_ID,
    parameters: {
      max_new_tokens: 100,
      temperature: 0.7,
      top_p: 0.9
    }
  });

  console.log(response.results[0].generated_text);
  ```
</CodeGroup>

### Selective Instrumentation

Control which integrations are enabled using the `instruments` or `blockInstruments` configuration:

<CodeGroup>
  ```python Python theme={null}
  from netra import Netra
  from netra.instrumentation.instruments import InstrumentSet

  # Only enable Watsonx instrumentation
  Netra.init(
      headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
      instruments={InstrumentSet.WATSONX}
  )

  # Or block specific instrumentations
  Netra.init(
      headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
      block_instruments={InstrumentSet.HTTPX}
  )
  ```

  ```typescript Typescript theme={null}
  import { Netra, NetraInstruments } from "netra-sdk";

  // Only enable Watsonx instrumentation
  await Netra.init({
    headers: `x-api-key=${process.env.NETRA_API_KEY}`,
    instruments: new Set([NetraInstruments.WATSONX])
  });

  // Or block specific instrumentations
  await Netra.init({
    headers: `x-api-key=${process.env.NETRA_API_KEY}`,
    blockInstruments: new Set([NetraInstruments.HTTP])
  });
  ```
</CodeGroup>

## Next Steps

* [Quick Start Guide](https://docs.getnetra.ai/quick-start/python) - Complete setup and configuration
* [Auto Instrumentation](https://docs.getnetra.ai/tracing/auto-instrumentation) - Automatic tracing for supported libraries
* [Decorators](https://docs.getnetra.ai/tracing/decorators) - Add custom tracing with `@workflow`, `@agent`, and `@task` decorators
* [Session Tracking](https://docs.getnetra.ai/tracing/session) - Track user sessions and conversations
* [Watsonx Documentation](https://www.ibm.com/docs/en/watsonx-as-a-service) - Official IBM Watsonx documentation
