# Context Engineering

> The Context Engineering is the practice of selecting and organizing all information provided to an AI model for a task.

## What is context engineering vs prompt engineering?

The practice of deciding what goes into the context window: instructions, relevant files, documentation, tool results and history. Since the window is limited, giving the right information matters more than the wording of the prompt.

- Topic: Prompting
- Type: practice

## Example

Wrong:

```js
const files = await readAllFiles('src');
const messages = [
  { role: 'user', content: files.join('\n') + task },
];
```

Right:

```js
const files = await searchRelevantFiles('src', task, { limit: 5 });
const context = files.map((f) => `<file path="${f.path}">\n${f.text}\n</file>`);
const messages = [
  { role: 'user', content: `${context.join('\n')}\n<task>${task}</task>` },
];
```

A few relevant files, tagged as data, keep the context small and focused, which gives better answers.

## Common mistakes

- Dumping every file into the context and getting worse answers, not better ones.

## Key points

- Only documents and tool results relevant to the current task are added to the context.
- Long tool outputs are trimmed or summarized before they go back to the model.
- Retrieved or external text is marked as data, separate from instructions.
- The total context size is measured and kept under a set limit.

## Interview questions

### Context engineering vs prompt engineering?

Prompt engineering shapes the instructions. Context engineering decides everything the model sees: instructions, retrieved docs, tool results, memory and history.

### Why do answers get worse with more context?

Long contexts dilute attention, and key facts get lost among irrelevant text. Models often use information in the middle of a long context less well.

### How do I keep an agent's context small?

Trim or summarize long tool outputs, drop old steps that no longer matter, and load documents only when the task needs them.

## Related cards

- [Prompt Engineering](https://wonderium.dev/ai/prompt-engineering.md)
- [Context Window](https://wonderium.dev/ai/context-window.md)
- [Context Rot](https://wonderium.dev/ai/context-rot.md)
- [RAG](https://wonderium.dev/ai/rag.md)
- [Context Compaction](https://wonderium.dev/ai/context-compaction.md)

---

- Level: [Artificial Intelligence](https://wonderium.dev/ai.md)
- HTML version: https://wonderium.dev/ai/context-engineering
- Last updated: 2026-11-25
