Demystifying Tokens & Temperature: Stop Feeling Lost in the LLM Jungle


Sometime or in the beginning the world of LLMs feels like trying to order coffee in a foreign language – you know what you want, but the terminology makes your brain hurt.
Let's fix that with some real examples.
Tokens: The LEGO Blocks of AI Language
Tokens are how LLMs chop up text. Not words, not characters – something in between that makes perfect sense to robots.
Real Examples:
"Hello world" = 2 tokens
"I'm learning about AI" = 5 tokens
"Understanding" = 2 tokens ("Under" + "standing")
"Don't" = 2 tokens ("Don" + "'t")
Quick Math: ~4 tokens per word, so your 100-word email costs about 400 tokens.
# Using OpenAI's tiktoken library
import tiktoken
encoding = tiktoken.get_encoding("cl100k_base")
text = "Hello! How are you doing today?"
tokens = encoding.encode(text)
print(f"Tokens: {len(tokens)}") # Output: Tokens: 7
Temperature: Your AI's Personality Dial
Think of temperature like a creativity slider – 0 is robot mode, 1 is jazz musician.
Example Prompt: "Describe a sunset"
Temperature 0.2 (Boring but Reliable):
The sun sets in the west, creating orange and red colors in the sky as daylight fades.
Temperature 0.8 (Creative Chaos):
The sky bleeds tangerine dreams while the sun melts like cosmic butter across the horizon,
whispering secrets to the waiting stars.
Code Example:
# OpenAI API example
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": "Write a haiku about coffee"}],
temperature=0.2 # Play with this: 0.0-1.0
)
Cost Control: Token Ninja Techniques
1. Prompt Surgery
Bad (Expensive): "Tell me everything about cats" Good (Cheap): "List 5 cat breeds under 50 words"
2. Context Limits
# Limit conversation history
messages = conversation_history[-5:] # Only last 5 messages
3. Iterative Approach
Instead of: "Write a complete marketing plan" Try:
"Outline a marketing plan in 5 bullet points"
"Expand bullet point 1 into a paragraph"
"Add 3 specific tactics for social media"
Real Cost Example:
Novel-length prompt: 2,000 tokens = $0.004 (GPT-3.5)
Surgical prompt: 200 tokens = $0.0004
10x savings with better prompting!
Subscribe to my newsletter
Read articles from Alankar Srivastava directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
