# Token Counting

**Package:** Anthropic Laravel | **Version:** 1 | **URL:** https://mozex.dev/docs/anthropic-laravel/v1/usage/token-counting

---

The token counting endpoint tells you how many input tokens a message would use without actually creating it. Useful for cost estimation, staying within context limits, or trimming conversation history before sending.

## Counting tokens

```php
use Anthropic\Laravel\Facades\Anthropic;

$response = Anthropic::messages()->countTokens([
    'model' => 'claude-sonnet-4-6',
    'messages' => [
        ['role' => 'user', 'content' => 'Hello, world'],
    ],
]);

$response->inputTokens; // 2095
```

Same parameters as `create()`, but it doesn't generate a response. You can include `tools`, `system`, and anything else that contributes to the token count.

## Trimming conversation history

A common Laravel pattern is to count tokens before sending and trim older messages if you're close to the context limit:

```php
$messages = $conversation->messages()
    ->latest('id')
    ->take(50)
    ->get()
    ->reverse()
    ->map(fn ($message) => [
        'role' => $message->is_assistant ? 'assistant' : 'user',
        'content' => $message->content,
    ])
    ->toArray();

$count = Anthropic::messages()->countTokens([
    'model' => 'claude-sonnet-4-6',
    'messages' => $messages,
]);

if ($count->inputTokens > 180000) {
    // Drop the oldest pairs until we're under the limit
    $messages = array_slice($messages, 4);
}

$response = Anthropic::messages()->create([
    'model' => 'claude-sonnet-4-6',
    'max_tokens' => 1024,
    'messages' => $messages,
]);
```

## Cost estimation before sending

If you bill users for Claude usage, count tokens first and show them an estimate:

```php
$count = Anthropic::messages()->countTokens([
    'model' => $request->input('model'),
    'system' => $request->input('system'),
    'messages' => $request->input('messages'),
]);

$estimatedCost = $count->inputTokens * $inputRatePerToken;

return response()->json([
    'estimated_input_tokens' => $count->inputTokens,
    'estimated_cost' => $estimatedCost,
]);
```

---

For the full request specification, see the [Token Counting page in the PHP docs](https://mozex.dev/docs/anthropic-php/v1/usage/token-counting) or the [Count Tokens API reference](https://platform.claude.com/docs/en/api/messages/count_tokens) on the Anthropic docs.

---

## Table of Contents

- [Introduction](https://mozex.dev/docs/anthropic-laravel/v1)
- [AI Integration](https://mozex.dev/docs/anthropic-laravel/v1/ai-integration)
- [Support Us](https://mozex.dev/docs/anthropic-laravel/v1/support-us)
- [Requirements](https://mozex.dev/docs/anthropic-laravel/v1/requirements)
- [Changelog](https://mozex.dev/docs/anthropic-laravel/v1/changelog)
- [Contributing](https://mozex.dev/docs/anthropic-laravel/v1/contributing)
- [Questions & Issues](https://mozex.dev/docs/anthropic-laravel/v1/questions-and-issues)
- [About Mozex](https://mozex.dev/docs/anthropic-laravel/v1/about)

### Usage

- [Messages](https://mozex.dev/docs/anthropic-laravel/v1/usage/messages)
- [Streaming](https://mozex.dev/docs/anthropic-laravel/v1/usage/streaming)
- [Tool Use](https://mozex.dev/docs/anthropic-laravel/v1/usage/tool-use)
- [Thinking](https://mozex.dev/docs/anthropic-laravel/v1/usage/thinking)
- [Server Tools](https://mozex.dev/docs/anthropic-laravel/v1/usage/server-tools)
- [Citations](https://mozex.dev/docs/anthropic-laravel/v1/usage/citations)
- [Token Counting](https://mozex.dev/docs/anthropic-laravel/v1/usage/token-counting)
- [Models](https://mozex.dev/docs/anthropic-laravel/v1/usage/models)
- [Batches](https://mozex.dev/docs/anthropic-laravel/v1/usage/batches)
- [Completions](https://mozex.dev/docs/anthropic-laravel/v1/usage/completions)

### Reference

- [Configuration](https://mozex.dev/docs/anthropic-laravel/v1/reference/configuration)
- [Error Handling](https://mozex.dev/docs/anthropic-laravel/v1/reference/error-handling)
- [Meta Information](https://mozex.dev/docs/anthropic-laravel/v1/reference/meta-information)
- [Testing](https://mozex.dev/docs/anthropic-laravel/v1/reference/testing)