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

# Instrument your code

> Learn how to trace and publish data to Openlayer

To monitor your AI system in production, Openlayer needs to *see* the requests it is handling.
**Instrumenting your code with Openlayer's SDKs** is how you make that happen.

Once done, you will be able to view traces and set up tests that run continuously on top of them.

> [!NOTE]
>
> This guide covers LLM and agent applications, which are instrumented with
> tracing. Monitoring a **traditional / tabular ML model** (e.g., scikit-learn
> classification or regression)? There is no LLM call to trace — see [Publish
> traditional ML predictions](https://docs.openlayer.com/monitoring/publishing-tabular-predictions.md)
> instead.

## How to integrate

> [!NOTE]
>
> **Prerequisites**:
>
> * A [project](https://docs.openlayer.com/workspace-and-projects/creating-and-loading-projects.md) in Openlayer with monitoring mode enabled.
>
> * An [Openlayer API key](https://docs.openlayer.com/workspace-and-projects/find-your-api-key.md).
>
> * The [Openlayer SDK](https://docs.openlayer.com/api-reference/sdk/overview.md) in your language of choice installed.

The canonical recipe to integrate is:

**1. Configure credentials**

Set the following environment variables to tell the Openlayer SDKs where to upload
captured the traces:

```bash theme={null}
OPENLAYER_API_KEY=YOUR_OPENLAYER_API_KEY
OPENLAYER_INFERENCE_PIPELINE_ID=YOUR_OPENLAYER_INFERENCE_PIPELINE_ID
```

**2. Instrument the code you want to trace**

Annotate all the functions you want to trace with Openlayer's SDK.

```python Python theme={null}
import openai
from openlayer.lib import init, trace

# Auto-instrument the installed LLM SDKs (OpenAI, etc.)
init()

openai_client = openai.OpenAI()  # auto-traced by Openlayer

# Decorate all the functions you want to trace
@trace()
def main(user_query: str) -> str:
    context = retrieve_context(user_query)
    answer = generate_answer(user_query, context)
    return answer

@trace()
def retrieve_context(user_query: str) -> str:
    return "Some context"

@trace()
def generate_answer(user_query: str, context: str) -> str:
    result = openai_client.chat.completions.create(
        messages=[{"role": "user", "content": user_query + " " + context}],
        model="gpt-4o"
    )
    return result.choices[0].message.content
```

> [!NOTE]
>
> **Not using OpenAI?** The steps are [similar for other LLM providers and frameworks](#framework-integrations).

**3. Use the instrumented code**

All data that goes through the instrumented code is automatically
sent to the Openlayer platform, where your tests and alerts are defined.

In the example above, if we call `main`:

```python Python theme={null}
main("what is the meaning of life?")
```

```typescript TypeScript theme={null}
tracedMain('what is the meaning of life?').catch(console.error);
```

the resulting trace is:

![Trace](https://mintcdn.com/openlayer-44/GsdFi44ahVZcKiSJ/images/monitoring/trace.png?fit=max\&auto=format\&n=GsdFi44ahVZcKiSJ\&q=85\&s=34aaee11b8f65450924815011c4451f3)

Note how the `main` function has two nested steps: `retrieve_context`, and
`generate_answer`. The `generate_answer` has a chat completion call within it. The cost,
number of tokens, latency, and other metadata are all captured automatically.

## Framework integrations

In the example above, we wrapped an **OpenAI** client.
If you are using a different provider or framework, the process is the same but
the wrapper might be different.

Pick your stack below for the exact snippet:

> [card] [OpenAI](https://docs.openlayer.com/integrations/openai.md#monitoring)

> [card] [Azure OpenAI](https://docs.openlayer.com/integrations/openai.md#monitoring)

> [card] [Anthropic](https://docs.openlayer.com/integrations/anthropic.md#monitoring)

> [card] [LangChain](https://docs.openlayer.com/integrations/langchain.md#monitoring)

> [card] [LangGraph](https://docs.openlayer.com/integrations/langgraph.md#monitoring)

> [card] [Amazon Bedrock](https://docs.openlayer.com/integrations/amazon-bedrock.md#monitoring)

> [card] [LiteLLM](https://docs.openlayer.com/integrations/litellm.md#monitoring)

> [card] [Oracle OCI GenAI](https://docs.openlayer.com/integrations/oracle-oci.md#monitoring)

> [card] [Mistral AI](https://docs.openlayer.com/integrations/mistral-ai.md#monitoring)

> [card] [Groq](https://docs.openlayer.com/integrations/groq.md#monitoring)

> [card] [OpenAI Agents SDK](https://docs.openlayer.com/integrations/openai-agents-sdk.md#monitoring)

> [!NOTE]
>
> Don't see your framework? Check out the [Integrations
> page](https://docs.openlayer.com/integrations/overview.md) for more details or [reach
> out](mailto:support@openlayer.com).

## What the instrumentation is doing

When you integrate, you are telling Openlayer two things:

1. **“Here are my AI calls.”** These are handled by Openlayer SDKs integrations for
   frameworks like OpenAI, Anthropic, or LangChain. Leveraging them, you get automatic
   capture of inputs, outputs, tokens, costs, latency, model parameters, and more.

2. **“Here’s the rest of my workflow.”** (Optional)
   Retrieval steps, ranking, filtering, post-processing — anything that’s not an LLM call.
   These are marked with the `@trace` decorator, so they show up as steps in the same trace.

Together, wrappers and decorators give you a full picture: not just model calls,
but **how your whole system behaves in production.**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.