How to Learn Generative AI: Beginner to Production
Blog Post

How to Learn Generative AI: Beginner to Production

Jake McCluskey
Back to blog

Learning generative AI in the right order means starting with tokens and tokenization, moving through embeddings and vector search, building retrieval augmented generation (RAG) systems, constructing agents with proper evaluation loops, and finally deploying to production with observability and guardrails. Most developers learn backwards by jumping straight to agent frameworks without understanding why their systems hallucinate, cost too much, or break under real-world conditions. This guide walks you through each stage sequentially with runnable code examples using 2026-current tools, building the conceptual foundation you need to debug production issues and ship AI systems that actually work.

Why Most Developers Learn GenAI Backwards and Get Stuck

You copy a LangChain tutorial, spin up an agent, and it works. Then you try to adapt it for your use case and everything falls apart. The agent hallucinates. Costs spiral. Retrieval returns garbage. You don't know which layer is broken because you never learned how the layers actually work.

The typical learning path goes: find a framework tutorial, copy code, deploy, panic when production breaks. This approach skips the fundamentals that let you diagnose why a model is using 3,000 tokens when it should use 800, or why your vector search returns irrelevant documents 40% of the time. Understanding what is tokenization in LLMs and how it works isn't academic, it's the difference between a $500 monthly API bill and a $50 one.

Roughly 60% of developers building their first production AI system underestimate token costs by at least 5x in initial planning. They don't know that different tokenizers split text differently, that GPT-4 uses roughly 1.3 tokens per word while Claude uses closer to 1.1. Or that prompt caching can cut costs by 90% if you structure context correctly.

GenAI Fundamentals: Tokens, Embeddings, and RAG Explained

Start here: everything in an LLM is tokens. When you send "Hello world" to GPT-4, it gets split into tokens (probably 2: "Hello" and " world"). The model processes these as numbers, runs them through attention layers, and predicts the next token. That's it. That's the whole system.

Here's how to see tokenization in action with OpenAI's tiktoken library:


import tiktoken

encoding = tiktoken.encoding_for_model("gpt-4")
text = "Hello world! How are you today?"
tokens = encoding.encode(text)

print(f"Text: {text}")
print(f"Tokens: {tokens}")
print(f"Token count: {len(tokens)}")
print(f"Decoded: {[encoding.decode([t]) for t in tokens]}")

This shows you exactly how your input gets chunked. Run this on your actual prompts and you'll immediately see why some phrasings cost 30% more than others. Different models use different tokenizers, so a prompt optimized for GPT-4 might be inefficient for Claude or Llama.

Embeddings come next. These are dense vector representations of text that capture semantic meaning. When you convert "dog" and "puppy" to embeddings using a model like OpenAI's text-embedding-3-small, the resulting vectors sit close together in high-dimensional space (1536 dimensions for that model). Understanding what are embeddings in AI and how they work is critical before you build any RAG system.


from openai import OpenAI
client = OpenAI()

def get_embedding(text):
    response = client.embeddings.create(
        model="text-embedding-3-small",
        input=text
    )
    return response.data[0].embedding

dog_embedding = get_embedding("dog")
puppy_embedding = get_embedding("puppy")
car_embedding = get_embedding("car")

# Calculate cosine similarity
import numpy as np
from numpy.linalg import norm

def cosine_similarity(a, b):
    return np.dot(a, b) / (norm(a) * norm(b))

print(f"dog vs puppy: {cosine_similarity(dog_embedding, puppy_embedding):.3f}")
print(f"dog vs car: {cosine_similarity(dog_embedding, car_embedding):.3f}")

You'll see dog/puppy scores around 0.85+ while dog/car sits closer to 0.3. That's semantic search in action. This is the foundation of RAG: you embed your documents, store them in a vector database, and retrieve the most similar ones when a user asks a question.

How to Build Retrieval Augmented Generation (RAG) Systems That Actually Work

RAG solves the knowledge cutoff problem by retrieving relevant documents and injecting them into the prompt. Basic RAG is simple: embed documents, store in a vector database like pgvector or Pinecone, retrieve top-k similar documents for each query, stuff them into the prompt.

Here's a minimal RAG implementation using pgvector:


import psycopg2
from pgvector.psycopg2 import register_vector

conn = psycopg2.connect(database="your_db")
conn.autocommit = True
register_vector(conn)

# Create table with vector column
cur = conn.cursor()
cur.execute("CREATE EXTENSION IF NOT EXISTS vector")
cur.execute("CREATE TABLE IF NOT EXISTS documents (id SERIAL PRIMARY KEY, content TEXT, embedding vector(1536))")

# Insert document with embedding
content = "Your document text here"
embedding = get_embedding(content)
cur.execute("INSERT INTO documents (content, embedding) VALUES (%s, %s)", (content, embedding))

# Query for similar documents
query = "User question here"
query_embedding = get_embedding(query)
cur.execute("SELECT content FROM documents ORDER BY embedding <-> %s LIMIT 3", (query_embedding,))
results = cur.fetchall()

But basic RAG fails in production. Retrieval quality matters more than model choice for most applications. If you retrieve irrelevant documents, even GPT-4 will give bad answers. Hybrid search (combining vector similarity with keyword matching) typically improves results by 20-35% compared to pure vector search.

Reranking adds another layer: retrieve 20 candidates with fast vector search, then use a cross-encoder model to rerank the top 5. Models like Cohere's rerank-english-v3.0 or bge-reranker-large can boost relevance significantly. Testing with actual user queries shows that reranking can improve answer quality by roughly 25% as measured by human preference ratings.

You should also learn when RAG isn't the answer. For more details on fixing poor retrieval, check out how to fix bad RAG retrieval results with hybrid search.

How to Build AI Agents from Scratch Step by Step

An agent is just a loop: observe, think, act, repeat. The "thinking" part is an LLM deciding what to do next. The "acting" part is calling tools (functions, APIs, databases). Don't start with frameworks. Build the loop yourself first so you understand what's actually happening.

Here's a minimal agent that can use tools:


import json
from openai import OpenAI
client = OpenAI()

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {"type": "string", "description": "City name"}
                },
                "required": ["location"]
            }
        }
    }
]

def get_weather(location):
    # Mock function - replace with real API
    return f"Weather in {location}: 72°F, sunny"

def run_agent(user_message):
    messages = [{"role": "user", "content": user_message}]
    
    for i in range(5):  # Max 5 iterations to prevent infinite loops
        response = client.chat.completions.create(
            model="gpt-4",
            messages=messages,
            tools=tools
        )
        
        message = response.choices[0].message
        
        if not message.tool_calls:
            return message.content
        
        # Execute tool calls
        messages.append(message)
        for tool_call in message.tool_calls:
            function_name = tool_call.function.name
            arguments = json.loads(tool_call.function.arguments)
            
            if function_name == "get_weather":
                result = get_weather(arguments["location"])
            
            messages.append({
                "role": "tool",
                "tool_call_id": tool_call.id,
                "content": result
            })
    
    return "Agent exceeded max iterations"

print(run_agent("What's the weather in Seattle?"))

This is the core pattern. The agent calls the model, checks if it wants to use tools, executes those tools, feeds results back, and repeats until the model returns a final answer. Everything else (ReAct, plan-and-execute, multi-agent systems) is variations on this loop.

Model Context Protocol (MCP) is emerging as the standard way to connect agents to tools in 2026. Instead of writing custom tool integrations, you connect to MCP servers that expose standardized tool interfaces. Anthropic released the spec in late 2024 and adoption is growing fast among developers building production agents.

The key to reliable agents is understanding when they fail. Agents break when they lose context (forgetting earlier steps), make bad tool choices (calling the wrong function), or hit token limits mid-task. Building agents from scratch teaches you to spot these failure modes before they hit production. For more advanced patterns, see how to choose the right AI agent architecture for your project.

What to Learn Before Building AI Agents

You need three foundations before agents make sense: prompt engineering, model selection, and cost management. Prompt engineering isn't about magic phrases. It's about structure, examples, and clear instructions.

The most reliable prompting pattern is structured output with examples:


system_prompt = """You are a data extraction assistant. Extract structured information from text.

Examples:

Input: "John Smith works at Acme Corp as a Senior Engineer. Email: [email protected]"
Output: {"name": "John Smith", "company": "Acme Corp", "title": "Senior Engineer", "email": "[email protected]"}

Input: "Contact Sarah at [email protected]. She's the Marketing Director at TechStart."
Output: {"name": "Sarah", "company": "TechStart", "title": "Marketing Director", "email": "[email protected]"}

Now extract from the following text. Return only valid JSON."""

user_prompt = "Mike Johnson is the CTO at DataCo. Reach him at [email protected]"

response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_prompt}
    ],
    response_format={"type": "json_object"}
)

Model selection matters more in 2026 than ever. GPT-4 costs roughly 10x more than GPT-3.5, but you don't always need it. Route simple queries to cheaper models and complex ones to expensive models. This routing can cut costs by 5-8x while maintaining quality.

Here's a simple routing strategy:


def route_query(query):
    # Use cheap model to classify complexity
    classification = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{
            "role": "user",
            "content": f"Is this query simple (yes/no)? Query: {query}"
        }]
    )
    
    is_simple = "yes" in classification.choices[0].message.content.lower()
    model = "gpt-3.5-turbo" if is_simple else "gpt-4"
    
    return client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": query}]
    )

Context management is the third foundation. Models have token limits (128k for GPT-4 Turbo, 200k for Claude 3.5 Sonnet), but using the full context window costs a fortune. Learn to summarize old messages, cache static context, and keep prompts tight. A well-structured 2,000-token prompt often outperforms a sloppy 10,000-token one.

Generative AI Learning Roadmap for Developers in 2026

Here's the complete learning sequence with time estimates. Expect 3-4 months of focused learning to go from zero to production-ready, assuming you code 10-15 hours per week.

Week 1-2: Tokens and Tokenization
Install tiktoken, test different models' tokenizers, calculate token costs for real prompts. Build a token counter tool for your own use. Developers who understand tokenization save an average of 35% on API costs compared to those who don't.

Week 3-4: Embeddings and Vector Search
Generate embeddings with OpenAI or Cohere, store them in pgvector or Pinecone, build a semantic search system over your own documents. Test different embedding models (text-embedding-3-small vs text-embedding-3-large) and measure retrieval quality.

Week 5-7: RAG Systems
Build basic RAG, add hybrid search (vector + keyword), implement reranking, test chunking strategies (500 tokens vs 1000 tokens vs semantic chunking). Measure retrieval precision on a test set of 50+ queries.

Week 8-10: Agents and Tool Use
Build the basic agent loop from scratch, add 3-5 tools, implement error handling and retry logic. Test with complex multi-step tasks. Don't use frameworks yet.

Week 11-12: Evaluation and Observability
Write evals that test your agent on real tasks, add logging (LangSmith or custom), track token usage per task, measure success rates. Good evals are the difference between shipping broken agents and shipping reliable ones.

Week 13-14: Production Deployment
Add guardrails (content filtering, PII detection), implement rate limiting and caching, monitor latency and costs, set up alerts. Test under load.

Use current tools: OpenAI SDK, Anthropic SDK, LangChain (only after you understand the basics), LlamaIndex for advanced RAG, Instructor for structured outputs, LiteLLM for model routing. The best Python libraries for 2026 are covered in best Python libraries for building generative AI apps.

Production Essentials: Evals, Observability, and Deployment

Evaluation is non-negotiable. You can't improve what you don't measure. Build a test set of 50-100 realistic queries with expected outputs. Run your system against this set after every change.


test_cases = [
    {"query": "What's the return policy?", "expected_answer": "30 days"},
    {"query": "Do you ship internationally?", "expected_answer": "Yes, to 50+ countries"},
    # ... 48
Ready to stop reading and start shipping?

Get a free AI-powered SEO audit of your site

We'll crawl your site, benchmark your local pack, and hand you a prioritized fix list in minutes. No call required.

Run my free audit
WANT THE SHORTCUT

Need help applying this to your business?

The post above is the framework. Spend 30 minutes with me and we'll map it to your specific stack, budget, and timeline. No pitch, just a real scoping conversation.