Normandy.Context.TokenCounter (normandy v0.2.0)

View Source

Provides token counting utilities using the Anthropic token counting API.

This module wraps Claudio's token counting functionality to provide accurate token counts for messages and conversations.

Example

# Count tokens for a single message
{:ok, count} = TokenCounter.count_message(client, "Hello, world!")

# Count tokens for an entire conversation
{:ok, count} = TokenCounter.count_conversation(client, agent)

# Get detailed token breakdown
{:ok, details} = TokenCounter.count_detailed(client, agent)

Summary

Functions

Counts tokens for an agent's conversation history.

Gets detailed token breakdown for an agent's conversation.

Functions

count_conversation(client, agent)

@spec count_conversation(
  Normandy.LLM.ClaudioAdapter.t(),
  struct()
) :: {:ok, map()} | {:error, term()}

Counts tokens for an agent's conversation history.

Example

{:ok, count} = TokenCounter.count_conversation(client, agent)
#=> {:ok, %{"input_tokens" => 1234}}

count_detailed(client, agent)

@spec count_detailed(
  Normandy.LLM.ClaudioAdapter.t(),
  struct()
) :: {:ok, map()} | {:error, term()}

Gets detailed token breakdown for an agent's conversation.

Returns total input tokens and estimates for system/user/assistant messages.

Example

{:ok, details} = TokenCounter.count_detailed(client, agent)
#=> {:ok, %{
  total_tokens: 1234,
  system_tokens: 100,
  message_tokens: 1134,
  messages: [
    %{role: "user", content: "Hello", estimated_tokens: 4},
    %{role: "assistant", content: "Hi!", estimated_tokens: 3}
  ]
}}

count_message(client, text, model \\ "claude-3-5-sonnet-20241022")

@spec count_message(Normandy.LLM.ClaudioAdapter.t(), String.t(), String.t()) ::
  {:ok, map()} | {:error, term()}

Counts tokens for a single text message.

Uses the Anthropic API for accurate token counting.

Example

{:ok, count} = TokenCounter.count_message(client, "Hello, world!")
#=> {:ok, %{"input_tokens" => 4}}