• EnglishEspañol日本語한국어Português
  • Log inStart now

WithLlmCustomAttributes (Python agent API)

Syntax

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

Python agent version 10.1.0 or higher.

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.

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

None.

Examples

Add custom attributes to an OpenAI chat completion call

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",
)
Copyright © 2024 New Relic Inc.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.