Skip to content

Latest commit

 

History

History
84 lines (64 loc) · 2.19 KB

setllmtokencountingcallback-net-agent-api.mdx

File metadata and controls

84 lines (64 loc) · 2.19 KB
title type shortDescription tags metaDescription redirects freshnessValidatedDate
SetLlmTokenCountingCallback (.NET agent API)
apiDoc
Provide a callback method that determines the token count for an LLM completion
Agents
NET agent
NET agent API
New Relic .NET agent API call to provide a callback method that determines the token count for an LLM completion.
/docs/agents/net-agent/net-agent-api/setllmtokencountingcallback-net-agent-api
/docs/agents/net-agent/net-agent-api/setllmtokencountingcallback-net-agent
/docs/agents/net-agent/net-agent-api/set-llm-token-counting-callback
never

Syntax

NewRelic.Api.Agent.NewRelic.SetLlmTokenCountingCallback(Func<string, string, int> callback);

Provide a callback method that calculates the token count.

Requirements [#requirements]

This API call is compatible with:

  • Agent versions >= 10.23.0
  • All app types

Description [#description]

Set a callback method that the agent will use to determine the token count for a LLM event. In High Security Mode or when content recording is disabled, this method will be called to determine the token count for the LLM event.

The callback method must accept a two arguments of type string, and return an integer. The first string argument is the LLM model name, and the second string argument is the input to the LLM. The callback method should return the token count for the LLM event. Values of 0 or less will be ignored.

Parameters

  <th>
    Description
  </th>
</tr>
Parameter
`$callback`
    {'_Func<string, string, int>_'}
    
  </td>

  <td>
    The callback to determine the token count.
  </td>
</tr>

Example

Func<string, string, int> llmTokenCountingCallback = (modelName, modelInput) => {

  int tokenCount = 0;
  // split the input string by spaces and count the tokens
  if (!string.IsNullOrEmpty(modelInput))
  {
    tokenCount = modelInput.Split(' ').Length;
  }

	return tokenCount;
};

NewRelic.Api.Agent.NewRelic.SetLlmTokenCountingCallback(llmTokenCountingCallback);