Estimate token usage for Google Vertex AI models before sending requests using Portkey’s CountTokens endpoint.
Portkey supports the Google Vertex AI CountTokens API, which returns the token count for a given input. This helps you estimate token usage before sending requests to models, allowing you to estimate costs, optimize prompts to fit within token limits, and plan for token usage in your applications.The token count returned by this operation matches the token count that would be charged if the same input were sent to the model in a generation request.
Portkey supports Vertex AI’s CountTokens endpoint through the Anthropic-compatible format, allowing you to use the same API signature across Vertex AI, Bedrock, and Anthropic providers.
import anthropicclient = anthropic.Anthropic( api_key="dummy", # we will use portkey's provider slug default_headers={"x-portkey-api-key": "YOUR_PORTKEY_API_KEY"}, base_url="https://api.portkey.ai")response = client.messages.count_tokens( model="@your-vertex-provider-slug/your-model-name", system="You are a scientist", messages=[{ "role": "user", "content": "Hello, Claude" }],)print(response.json())
import Anthropic from '@anthropic-ai/sdk';const client = new Anthropic({ apiKey: 'dummy', // we will use portkey's provider slug baseURL: "https://api.portkey.ai/v1", defaultHeaders: { "x-portkey-api-key": "YOUR_PORTKEY_API_KEY" }});const response = await client.messages.countTokens({ model: '@your-vertex-provider-slug/your-model-name', system: 'You are a scientist', messages: [{ role: 'user', content: 'Hello, Claude' }]});console.log(response);