LLMs Explained for Developers: Tokens, Context, Embeddings

Artificial Intelligence and Large Language Models (LLMs) have transformed from experimental research models into foundational building blocks for modern software engineering. Whether building automated code assistants, intelligent customer support agents, or semantic search engines, developers are expected to integrate generative AI capabilities into production applications seamlessly.

However, treating LLMs merely as black-box REST APIs often leads to unexpected token costs, context window overflow errors, and hallucinated responses. This comprehensive guide breaks down the core internal primitives of LLMs from a developer's perspective: **Tokens**, **Context Windows**, **Vector Embeddings**, and **Retrieval-Augmented Generation (RAG)**.

1. Tokens: The Fundamental Unit of LLM Processing

LLMs do not process raw text characters or words directly. Instead, they operate on **tokens**—sub-word chunks generated by tokenizers using algorithms like Byte-Pair Encoding (BPE).

  • Token Ratio: As a general rule of thumb in English, 1 token is roughly equivalent to 4 characters or 0.75 words (100 tokens $\approx$ 75 words).
  • Pricing & Limits: Both API pricing models (cost per 1,000 tokens) and hardware inference speed are directly governed by token counts.
  • Code Tokenization: Programming languages typically tokenize at a higher rate than natural language due to special symbols, indentation, and CamelCase identifiers.

2. Context Windows: Managing Memory and State

The **Context Window** represents the maximum number of tokens (both input prompt and generated output combined) that an LLM can process in a single inference call.

  • Attention Mechanism: Transformer attention layers scale quadratically ($O(n^2)$) with respect to context length, though modern architectures use sparse attention and sliding windows to support millions of tokens.
  • State Management: Because LLMs are inherently stateless, conversation history must be explicitly appended to the prompt on every API request until the context window limit is reached.

3. Vector Embeddings and Semantic Search

An **embedding** is a dense vector of floating-point numbers (e.g., 1,536 dimensions) that represents the semantic meaning of text, code, or images in a high-dimensional vector space.

C
# Python OpenAI Embedding Generation Example
import openai

response = openai.embeddings.create(
    model="text-embedding-3-small",
    input="How do vector databases work in AI systems?"
)

vector_embedding = response.data[0].embedding
print(f"Embedding dimension length: {len(vector_embedding)}")
  • Semantic Proximity: Words or documents with similar meanings are mapped close to one another in vector space, measured using cosine similarity.
  • Vector Databases: Dedicated vector stores (like Pinecone, Milvus, Qdrant, or PostgreSQL with pgvector) index millions of embeddings for instant similarity searches.

4. Retrieval-Augmented Generation (RAG)

While LLMs are trained on vast public corpora, they lack access to private company codebases or real-time internal data. Fine-tuning models is expensive and prone to catastrophic forgetting. **Retrieval-Augmented Generation (RAG)** solves this by bridging vector search with LLM text generation.

C
User Query -> Embed Query -> Search Vector DB -> Retrieve Context Chunks -> Inject into LLM Prompt -> Generate Accurate Answer

Conclusion

Mastering LLM fundamentals—understanding how tokenization dictates cost, managing context windows efficiently, leveraging vector embeddings for semantic search, and architecting RAG pipelines—equips developers to build powerful, accurate, and cost-effective AI applications.