The month ends. The AI assistant invoice arrives. It is higher than expected. A solo developer feels that pain most. You regenerate a response a few times. The token count climbs fast. Free tiers try to help. But they still have hard limits. A 10-million-token allowance sounds generous. It vanishes quicker than you think.
Last month, a friend complained about an $80 overage fee. He had run a weekend experiment with long context. The assistant rephrased the same function twenty times. Each rephrase cost tokens. None of them improved the code. He had no record of the waste. A meter would have shown the spiral early.
Measurement prevents surprises. You need a metering layer between you and the model. That layer records every call. It estimates token consumption. It gives you a daily total. This article shows how to build one. MonkeyCode provides a free model endpoint and a free server tier. That combination is enough for a personal metering proxy. Disclosure: This article was prepared as part of MonkeyCode's product outreach.
Why a proxy instead of logging in the app? Many AI coding assistants hide usage statistics. They show a smooth interface. The numbers stay invisible. A proxy captures them in real time. It also builds a habit of conscious consumption. Think of it as a fuel gauge. You would not drive without one. Why code without a token meter?
The setup is a small Python service. It accepts your prompt and response. It estimates the token count. It appends the record to a local file. That file becomes your audit log. You can deploy this on MonkeyCode's free server tier. The following code is a minimal example. It is not production-grade. It works for personal use.
import json
from datetime import datetime
from flask import Flask, request, jsonify
app = Flask(__name__)
LOG_FILE = 'usage.log'
def estimate_tokens(text):
# Rough heuristic: one token per four characters
return len(text) // 4
def append_entry(entry):
with open(LOG_FILE, 'a') as f:
f.write(json.dumps(entry) + '\n')
@app.route('/log', methods=['POST'])
def log_usage():
payload = request.get_json()
prompt = payload.get('prompt', '')
response = payload.get('response', '')
tokens = estimate_tokens(prompt) + estimate_tokens(response)
entry = {
'time': datetime.utcnow().isoformat(),
'tokens': tokens
}
append_entry(entry)
return {'ok': True}, 200
@app.route('/summary', methods=['GET'])
def summary():
daily = {}
try:
with open(LOG_FILE, 'r') as f:
for line in f:
entry = json.loads(line)
day = entry['time'][:10]
daily[day] = daily.get(day, 0) + entry['tokens']
except FileNotFoundError:
pass
total = sum(daily.values())
return jsonify({
'daily': daily,
'total_tokens': total,
'limit': 10_000_000
}), 200
if __name__ == '__main__':
app.run()
The service has two endpoints. The first receives usage events. The second returns a summary. The token estimate uses a character heuristic. Actual tokenization varies by model. You can swap in a real tokenizer later. The important piece is the log. Each entry carries a timestamp and a token count. Over time, you get a time series.
Test it locally first. Run the server with python app.py. Then send a sample request.
curl -X POST http://localhost:5000/log \
-H "Content-Type: application/json" \
-d '{"prompt":"Write a function that sums a list","response":"def sum_list(x): return sum(x)"}'
Check the usage.log file. You should see one JSON line. The token count is roughly the length divided by four. That is enough for approximation.
Now deploy to a free server. MonkeyCode's free server tier can run a Flask app. Push the code to a repository. Follow the platform's deployment guide. You get a public URL. Use that URL in your AI workflow. Point your AI tool's proxy settings at it. The exact integration varies by tool. The core idea remains: intercept, measure, log.
The summary endpoint becomes your control panel. Visit https://your-server/summary any time. It shows a daily breakdown. It also shows your remaining allowance. You can set an alert by polling that endpoint. A simple cron job checks it weekly. The job sends a message when total consumption crosses a threshold. That threshold could be eighty percent of your limit. This way you act before the budget runs out.
Store the log in a separate directory. Create a gitignore entry for it. The log grows with every request. A month of daily solo work stays under a few megabytes. You can compress old entries. The summary endpoint reads the whole file. For heavy use, switch to SQLite. That change is straightforward.
This approach has limitations. The heuristic is not precise. It underestimates tokens for some languages. It overestimates for others. The free server may have cold starts. High-frequency requests will queue. Do not run this at scale. It serves a single developer. If you need exact counts, use the model's own tokenizer. If you need low latency, move to a paid tier.
Who should use this pattern? Solo developers on free plans. Indie hackers testing an idea. Anyone curious about their AI usage. If your team shares a billing account, use a real observability tool. This proxy is a personal instrument. It works best when you control the endpoint.
Try this pattern on your next side project. The setup takes twenty minutes. The insight lasts beyond the month. Your token budget becomes visible. You ship without anxiety.
Top comments (0)