πŸ“š Study Notes / Home / GenAI / Cheat Sheet
GenAI Β· 1-Page Cheat Sheet

GenAI β€” Quick Revision

Every session distilled: tokens & transformers, prompting, agents, RAG, frameworks, memory, voice, MCP, and deployment.

S1 How LLMs work

  • LLM/GPT = a next-token predictor: given text, it outputs a probability over the next token, samples one, appends, repeats (autoregressive).
  • Tokens = sub-word chunks (~4 chars). Text β†’ tokens β†’ embeddings (vectors capturing meaning). Positional encoding adds word order.
  • Attention (Q/K/V): each token's Query scores every Key; softmax weights mix the Values β†’ context-aware representation. The "Transformer" core.
  • Training = learn weights on huge text (slow, costly, frozen after). Inference = run frozen model to generate. Model has no memory + a knowledge cutoff.
  • Decoding knobs: temperature (↑ = more random), top-p (nucleus sampling), top-k, max_tokens (length cap). softmax turns logits β†’ probabilities.

S2 Prompt engineering

  • Roles: system (rules/persona), user (task), assistant (model replies). System steers behaviour.
  • Zero-shot = ask directly; few-shot = give examples in the prompt to set format/style.
  • Chain-of-Thought (CoT): "think step by step" β†’ better reasoning on multi-step problems.
  • Structured output: demand JSON/schema for reliable, parseable results.
  • Be specific: give context, constraints, examples, and an output format. Garbage in β†’ garbage out.

S3 Agents: giving AI hands

  • Agent = LLM + tools + a loop. Tools let it act (search, run code, call APIs) beyond its frozen knowledge.
  • Tool/function calling: model emits a structured call β†’ your code runs it β†’ result fed back β†’ model continues.
  • ReAct loop: Thought β†’ Action β†’ Observation, repeat until done. Reason and act interleaved.
  • Planning vs reactive: reactive = decide one step at a time; planning = lay out full steps first, then execute.
  • Agents add autonomy but cost more tokens/latency and can loop β€” cap iterations.

S4 RAG 1 β€” open book

  • RAG = Retrieval-Augmented Generation: fetch relevant docs, stuff into the prompt so the LLM answers from your data (beats cutoff + hallucination).
  • Chunking: split docs into passages (with overlap) so each fits and retrieves cleanly.
  • Embeddings: encode each chunk + the query as vectors; similar meaning β†’ near in space.
  • Vector DB (Pinecone, Chroma, pgvector): stores vectors, does fast similarity search (cosine / kNN).
  • Flow: chunk β†’ embed β†’ store, then embed query β†’ retrieve top-k β†’ generate with context.

S5 RAG 2 β€” making it good

  • Query rewriting: rephrase/expand the user query so retrieval matches better.
  • HyDE: generate a hypothetical answer, embed that, and retrieve with it.
  • Re-ranking: a cross-encoder re-scores top-k hits for relevance before generating.
  • CRAG (Corrective RAG): grade retrieved docs; if weak, fall back to web search / re-query.
  • Also: hybrid search (keyword + vector), metadata filtering, better chunking, citations to cut hallucination.

S6 LangChain & LangGraph

  • LangChain: framework to wire LLMs, prompts, tools, retrievers, memory into apps.
  • LCEL (LangChain Expression Language): compose components with | pipes β†’ runnable chains (stream/batch/async free).
  • LangGraph: model agents as a stateful graph of nodes + edges β†’ loops, branches, cycles.
  • Good for control over flow vs a single agent loop; supports human-in-the-loop and persistence.
  • Building blocks: prompt templates, output parsers, retrievers, tools, memory.

S7 Agent SDK

  • Agent SDK: opinionated, minimal way to build agents β€” define an agent with instructions + tools and run it; loop handled for you.
  • Lifecycle: input β†’ LLM picks tool β†’ tool runs β†’ result back β†’ repeat β†’ final output.
  • Handoffs: one agent delegates to another specialist agent (multi-agent orchestration).
  • Guardrails: validate inputs/outputs; structured outputs for typed results.
  • Less boilerplate than hand-wiring LangGraph for common agent patterns.

S8 Threads & Tracing

  • Thread = a conversation's running history so the agent keeps context across turns.
  • Tracing: record every LLM call, tool call, input/output, latency, token cost in a tree.
  • Observability (LangSmith, etc.): debug why an agent did X, find failures, measure cost & speed.
  • Spans nest under a trace; tag runs to filter and compare.
  • Essential for production: without traces, agents are black boxes.

S9 Agent memory

  • Model is stateless β€” memory is engineered around it.
  • Short-term = the context window: recent messages re-sent each turn (finite, costs tokens).
  • Long-term: persist facts in a DB / vector store and retrieve them when relevant.
  • Manage the window: summarize old turns, trim, or store + recall key facts.
  • Types: conversational buffer, summary memory, semantic (vector) memory.

S10 Graph DBs & Knowledge Graphs

  • Knowledge graph: data as nodes (entities) + edges (relationships) β€” stores connections, not just facts.
  • Neo4j = graph DB; query with Cypher to traverse relationships (multi-hop "who connects to what").
  • GraphRAG: retrieve over the graph for multi-hop / relational questions vectors miss.
  • Great when answers depend on how entities relate, not text similarity alone.

S11 Voice agents

  • Pipeline: STT (speechβ†’text) β†’ LLM (reason) β†’ TTS (textβ†’speech).
  • Latency is the enemy β€” every stage adds delay; aim for natural turn-taking.
  • Tricks: streaming partial transcripts/audio, interrupts (barge-in), VAD to detect speech end.
  • Speech-to-speech models cut hops vs the 3-stage pipeline.
  • Handle accents, noise, and conversational repair gracefully.

S12 MCP & A2A

  • MCP (Model Context Protocol): a standard way to expose tools/data to any LLM via tool servers β€” plug-and-play, no custom glue per app.
  • MCP server offers tools, resources, prompts; clients (agents) connect and call them.
  • A2A (Agent-to-Agent): protocol for agents to discover & talk to each other across systems.
  • Together: standardized tool access + agent interop β†’ composable ecosystems.

S13 Deployment

  • Ship the app as an API/service (e.g. on AWS); containerize + a managed model/API behind it.
  • Scaling: horizontal replicas + load balancing; handle bursty traffic.
  • Caching: cache embeddings & frequent LLM responses to cut cost + latency.
  • Rate limiting: protect against abuse and provider quota limits.
  • CI/CD for safe deploys; monitor cost, latency, errors; manage secrets/keys.