Introduction
In this tutorial, you'll learn how to work with OpenRouter, a platform that tracks and manages AI token usage. As reported in The Decoder, AI agents are now consuming more tokens than humans on OpenRouter, with agentic usage growing 14x since February 2025. This tutorial will teach you how to set up an OpenRouter account, understand token usage, and monitor your own AI interactions using the platform's API.
Prerequisites
Before starting this tutorial, you'll need:
- A computer with internet access
- A free account on OpenRouter (https://openrouter.ai)
- Basic understanding of what AI models are and how they work
- Python installed on your computer (any version 3.6 or higher)
Step-by-Step Instructions
1. Create Your OpenRouter Account
1.1 Visit the OpenRouter Website
Navigate to https://openrouter.ai in your web browser. This is where you'll manage your AI model usage and track token consumption.
1.2 Sign Up for an Account
Click on the "Sign Up" button and create an account using your email address. You'll receive a verification email to confirm your account.
1.3 Verify Your Email
Check your email inbox for a message from OpenRouter. Click the verification link to activate your account.
Why: You need an account to access the API and track token usage. OpenRouter is a platform that helps you monitor how much AI processing you're using.
2. Get Your API Key
2.1 Navigate to API Keys
After logging in, go to the "API Keys" section in your dashboard. This is where you'll find your unique authentication token.
2.2 Generate a New API Key
Click "Create New Key" and give it a descriptive name like "My AI Project". This key will be used to authenticate your requests to the OpenRouter API.
2.3 Copy Your API Key
Once generated, copy your API key. You'll need it in the next steps. Keep it secure - it's like a password for accessing the OpenRouter platform.
Why: The API key is essential for authenticating your requests to OpenRouter's services. Without it, you can't access the platform's features.
3. Set Up Your Development Environment
3.1 Create a New Python File
Create a new file called openrouter_demo.py in your preferred code editor. This will be where you write your Python code to interact with OpenRouter.
3.2 Install Required Libraries
Open your terminal or command prompt and run the following command to install the necessary Python libraries:
pip install requests python-dotenv
Why: The requests library lets us make HTTP calls to the OpenRouter API, while python-dotenv helps manage environment variables (like your API key) securely.
4. Configure Your Environment Variables
4.1 Create a .env File
In the same directory as your Python file, create a new file named .env. This file will store your API key securely.
4.2 Add Your API Key
Add the following line to your .env file:
OPENROUTER_API_KEY=your_actual_api_key_here
Replace your_actual_api_key_here with the API key you copied earlier.
4.3 Load Environment Variables
Update your openrouter_demo.py file with the following code:
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Get API key from environment
api_key = os.getenv('OPENROUTER_API_KEY')
print(f"API Key loaded: {api_key[:5]}...")
Why: Storing your API key in a separate file prevents it from being accidentally shared or committed to version control systems like GitHub.
5. Make Your First API Call
5.1 Create a Simple AI Request
Add the following code to your openrouter_demo.py file:
import requests
import json
# Define the API endpoint
url = "https://openrouter.ai/api/v1/chat/completions"
# Set up the headers with your API key
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# Define your prompt
prompt = "Explain what tokens are in AI language models in simple terms."
# Create the request payload
payload = {
"model": "openai/gpt-3.5-turbo",
"messages": [
{"role": "user", "content": prompt}
]
}
# Make the API call
response = requests.post(url, headers=headers, data=json.dumps(payload))
# Print the response
print("Response Status Code:", response.status_code)
print("Response Content:", response.json())
5.2 Run Your Python Script
Execute your script by running python openrouter_demo.py in your terminal. You should see a response with the AI-generated answer to your question.
Why: This step demonstrates how to interact with OpenRouter's API to get AI responses. Each time you make a request, tokens are consumed, which is tracked in your account.
6. Monitor Your Token Usage
6.1 Visit Your Dashboard
After running your script, go back to your OpenRouter dashboard. You should see your token usage increasing in real-time.
6.2 Understand the Usage Metrics
Look at the token consumption graph. Notice how each API call uses a certain number of tokens. The more complex your prompts and longer the responses, the more tokens are consumed.
Why: Monitoring your usage helps you understand how much your AI interactions cost and how efficiently you're using the platform.
7. Test Different AI Models
7.1 Modify the Model Parameter
Change the model parameter in your payload to try different AI models:
"model": "openai/gpt-4-turbo"
or
"model": "anthropic/claude-3-haiku"
7.2 Compare Token Usage
Run your script multiple times with different models and observe how token consumption varies. Different models may use different amounts of tokens for the same task.
Why: Different AI models have different token usage patterns. Understanding these differences helps you optimize your usage and costs.
Summary
In this tutorial, you've learned how to set up an OpenRouter account, obtain an API key, and make your first AI requests using Python. You've also learned how to monitor your token usage and experiment with different AI models. As reported in The Decoder, AI agents are now consuming more tokens than humans, and understanding this token consumption is key to managing costs and efficiency. By following these steps, you're now equipped to start tracking your own AI interactions and understanding how token usage works in practice.


