Short
What Is a Token? See It With One Support Ticket
About this short
Join the free Manifold AI Learning community for Agentic AI Learning Resources.
manifoldailearning.com/community
Code download: drive.google.com/file/d/16PBaAQjEwJI6f8sJQtAe0pUwvh1xMFmu/view
Day 1 of 100 Days of Agentic AI Concepts. Using OpenAI’s o200k_base encoding, the 17-word support ticket uses 24 tokens and the sample reply uses 59. At 10,000 tickets per day that is 240,000 input and 590,000 output tokens before instructions, history, tool data, or other overhead.
Reproduce the example locally (install tiktoken first with: python -m pip install tiktoken).
Save the code below as count_ticket_tokens.py and run: python count_ticket_tokens.py
"""Reproduce the support-ticket example from What Is a Token?"""
import json
import tiktoken
ENCODING = "o200k_base"
TICKET = (
"Hi, I returned my headphones on 25 Oct and I still haven't received "
"my refund. Order ORD-77812."
)
REPLY = (
"Thanks for reaching out about order ORD-77812. I can check whether we "
"received your headphones and when the refund is expected. If it has "
"already been issued, I’ll confirm the exact issue date. If the return "
"hasn’t been scanned yet, I’ll explain what we can do next."
)
ORDER_ID = "ORD-77812"
TICKETS_PER_DAY = 10_000
def measure():
encoding = tiktoken.get_encoding(ENCODING)
ticket_ids = encoding.encode(TICKET)
reply_ids = encoding.encode(REPLY)
order_ids = encoding.encode(ORDER_ID)
return {
"encoding": ENCODING,
"ticket": TICKET,
"words": len(TICKET.split()),
"ticket_tokens": [
part.decode("utf-8", errors="backslashreplace")
for part in encoding.decode_tokens_bytes(ticket_ids)
],
"order_tokens": [
part.decode("utf-8", errors="backslashreplace")
for part in encoding.decode_tokens_bytes(order_ids)
],
"reply": REPLY,
"input": len(ticket_ids),
"output": len(reply_ids),
"tickets_per_day": TICKETS_PER_DAY,
"daily_input": len(ticket_ids) * TICKETS_PER_DAY,
"daily_output": len(reply_ids) * TICKETS_PER_DAY,
}
if __name__ == "__main__":
print(json.dumps(measure(), ensure_ascii=False, indent=2))
#Shorts
What this short covers
- GenAI & LLMs — Generative AI and large language model fundamentals — transformers, embeddings, prompting, fine-tuning and how LLM APIs behave as distributed systems. All GenAI & LLMs videos →
Related searches
Go deeper on GenAI & LLMs