Normandy.Context.TokenCounter (normandy v0.2.0)
View SourceProvides 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.
Counts tokens for a single text message.
Functions
@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}}
@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}
]
}}
@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}}