Skip to content

Repository files navigation

overview

Context Engineering from Scratch

Understand deeply, then use frameworks wisely.

Context engineering decides what a model sees for one specific call. Not just the user's message - everything around it.

context engineering

This repo teaches that idea through 14 small, runnable examples. Each one shows exactly one piece of context and how it changes the model's answer. No frameworks, no production platform hiding behind the lessons - just Node.js and one local model, running on your machine.

The mental model

mental model

The examples build this diagram one box at a time, then assemble it.

Prerequisites

  • Node.js 22+ (uses the built-in node:sqlite module in example 12)
  • Basic JavaScript
  • min ~8-16 GB of RAM and ~3 GB of disk for the local GGUF models
  • No prior LLM experience, no API keys, no hosted services

Getting started

git clone https://github.com/your-fork/context-engineering-from-scratch
cd context-engineering-from-scratch
npm install
npm run download-model          # Qwen3.5 4B + BGE embeddings
npm run example -- 00

Every example is self-contained. Run any of them directly:

npm run example -- 04
node examples/04_long_term_memory/example.js   # equivalent

Examples 12-13 also save a context trace you can reopen afterward:

npm run context                    # shows the most recent trace
npm run context -- --id <id>       # shows a specific one

Repository layout

context-engineering-from-scratch/
├── README.md
├── DOWNLOAD.md
├── package.json
├── examples/            14 examples, 00-13
│   └── NN_topic/
│       ├── example.js   the runnable lesson (never over 150 lines)
│       ├── CONCEPT.md   the mental model - what this is, and isn't
│       └── CODE.md      what gets sent to the model, and why
├── helper/              shared code: model, CLI output, tokens, traces
├── scripts/             npm run example / context / download-model
├── data/traces/         saved context traces (examples 12-13)
└── models/              your local GGUF model goes here

There is no parallel src/ production application. What you read in an example is the whole idea - not a simplified preview of something bigger hiding elsewhere.

Learning path

Part 0 - The idea

# Example Docs What it makes visible
00 What Is Context Engineering? concept · code Same question, different context, different answer

Part 1 - How should the agent think?

# Example Docs What it makes visible
01 System Instructions concept · code Instructions shape behavior, not knowledge
02 Tool Definitions concept · code A tool definition steers the decision to act
03 Few-Shot Examples concept · code Examples demonstrate behavior; instructions describe it

Part 2 - What does the agent know?

# Example Docs What it makes visible
04 Long-Term Memory concept · code Memory lives outside the model until it's retrieved
05 External Knowledge with RAG concept · code Retrieval adds grounded facts the model didn't have
06 Tool Outputs concept · code A tool's result becomes knowledge, not just a decision
07 Sub-Agent Outputs concept · code A sub-agent's report becomes context for the next call
08 Artifacts concept · code A file only helps once something reads it

Part 3 - What must the agent do now?

# Example Docs What it makes visible
09 Conversation History concept · code History resolves what "it" or "which" refers to
10 State concept · code Structured state beats a wall of transcript
11 User Prompt concept · code The prompt decides which knowledge matters now

Part 4 - Putting the context together

# Example Docs What it makes visible
12 Context Orchestrator concept · code Question retrieves and scores; orchestrator packs what fits
13 Context Trace concept · code Inspect the context that produced an answer, not just the answer

What this repo is not

  • Not a production memory system, vector database, or RAG pipeline
  • Not a multi-agent orchestration framework
  • Not an evaluation or benchmarking platform
  • Not a scoring/ranking algorithm you should copy into production

Every example's CONCEPT.md ends with a Beyond this example note that sketches the production gap that example deliberately does not solve - what the toy mechanism is, what replaces it, and what adjacent concerns remain out of scope. Retrieval, memory, and multi-agent systems each deserve their own specialized resource - this one exists to build the mental model first.

License

MIT - see LICENSE.

Working with me

If this repo was useful and you have a project that needs ai workflows, context engineering, RAG, or local/production LLM work done properly, I'm open to freelance or contract engagements.

No pressure either way - glad if the repo was useful on its own.

About

Context engineering decides what a model sees. 14 self-contained examples that build the mental model one piece at a time, Node.js + local GGUF. Understand deeply, then use frameworks wisely. Hands-on context engineering - all local.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages