# Interface ITokenCounter

Interface for token counting operations.

interface ITokenCounter {
countAnthropic(
messages: Message[],
model: string,
): Promise<Result<TokenCountResult, TokenCountError>>;
countGemini(
content: string,
model: string,
): Promise<Result<TokenCountResult, TokenCountError>>;
countOpenAI(
text: string,
model?: string,
): Result<TokenCountResult, TokenCountError>;
estimate(text: string): number;
clearCache(): void;
getCacheStats(): { size: number; maxSize: number; ttlMs: number };
}

#### Implemented by

- TokenCounter

- Defined in packages/nexus-agents/src/context/token-counter-types.ts:94

##### Index

### Methods

countAnthropic
countGemini
countOpenAI
estimate
clearCache
getCacheStats

## Methods

### countAnthropic

- countAnthropic(
  messages: Message[],
  model: string,
  ): Promise<Result<TokenCountResult, TokenCountError>>

  Count tokens for Anthropic/Claude models via API.

  #### Parameters

  - messages: Message[]

    Messages to count tokens for

  - model: string

    Model identifier (e.g., 'claude-sonnet-4')

  #### Returns Promise<Result<TokenCountResult, TokenCountError>>

  Promise with token count result

  - Defined in packages/nexus-agents/src/context/token-counter-types.ts:101

### countGemini

- countGemini(
  content: string,
  model: string,
  ): Promise<Result<TokenCountResult, TokenCountError>>

  Count tokens for Gemini models via API.

  #### Parameters

  - content: string

    Text content to count tokens for

  - model: string

    Model identifier (e.g., 'gemini-2.0-flash')

  #### Returns Promise<Result<TokenCountResult, TokenCountError>>

  Promise with token count result

  - Defined in packages/nexus-agents/src/context/token-counter-types.ts:112

### countOpenAI

- countOpenAI(
  text: string,
  model?: string,
  ): Result<TokenCountResult, TokenCountError>

  Count tokens for OpenAI models using local tiktoken.

  #### Parameters

  - text: string

    Text to count tokens for

  - `Optional` model: string

    Model identifier (default: 'gpt-4o')

  #### Returns Result<TokenCountResult, TokenCountError>

  Token count result (synchronous, local)

  - Defined in packages/nexus-agents/src/context/token-counter-types.ts:120

### estimate

- estimate(text: string): number

  Estimate tokens offline using character-based heuristic.

  #### Parameters

  - text: string

    Text to estimate tokens for

  #### Returns number

  Estimated token count

  - Defined in packages/nexus-agents/src/context/token-counter-types.ts:127

### clearCache

- clearCache(): void

  Clear the token count cache.

  #### Returns void

  - Defined in packages/nexus-agents/src/context/token-counter-types.ts:132

### getCacheStats

- getCacheStats(): { size: number; maxSize: number; ttlMs: number }

  Get current cache statistics.

  #### Returns { size: number; maxSize: number; ttlMs: number }

  - Defined in packages/nexus-agents/src/context/token-counter-types.ts:137