---
title: WithLlmCustomAttributes (Python agent API)
source: https://docs.newrelic.com/docs/apm/agents/python-agent/python-agent-api/withllmcustomattributes-python-agent-api
---

## Syntax [#syntax]

```py
with newrelic.agent.WithLlmCustomAttributes(custom_attribute_map):
```

Context manager API that adds user-specified attributes to Large Language Model (LLM) events generated by LLM calls in application code.

## Requirements [#requirements]

Python agent version 10.1.0 or higher.

## Description [#description]

This context manager API adds custom user-specified attributes to each LLM event generated within its context based on calls made to LLMs.
The agent will automatically add an `llm.` prefix to each custom attribute key name specified in the passed in dictionary argument.
This API must be called within the context of an active transaction.

These custom attributes can be viewed on LLM events and queried for in the New Relic UI. For more information on AI Monitoring, see our [AI docs](https://docs.newrelic.com/docs/ai-monitoring/intro-to-ai-monitoring/).

## Parameters [#parameters]

| Parameter                           | Description                                                                                                              |
| ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| `custom_attribute_map` _dictionary_ | Required. A non-empty dictionary where each key-value pair indicates the custom attribute name and its respective value. |

## Return values [#return-values]

None.

## Examples [#examples]

### Add custom attributes to an OpenAI chat completion call

```py
import newrelic.agent

from openai import OpenAI

client = OpenAI()

with newrelic.agent.WithLlmCustomAttributes({"custom": "attr", "custom1": "attr1"}):
    response = client.chat.completions.create(
        messages=[{
            "role": "user",
            "content": "Say this is a test",
        }],
        model="gpt-4o-mini",
    )
```
