PTENES
Skip to content
MODULE 1 ADVANCED LEVEL

Context Engineering

Stop thinking of prompts as text. Learn to think of context as a system. The new technical core of modern prompt engineering.

6
Topics
90
Minutes
8
Exercises
1

Prompt as a Context System

The fundamental paradigm shift

The traditional view treats prompts as "commands" or "instructions." In modern context engineering, prompts are structured context systems that define the model’s complete operating environment. This paradigm shift is fundamental to working with advanced LLMs.

Paradigm Shift

❌ Old View

  • • Prompt = command text
  • • Focus on "how to ask"
  • • Linear instructions
  • • Unpredictable result

✅ New View

  • • Prompt = context system
  • • Focus on "operating environment"
  • • Structured layers
  • • Predictable behavior

Context as a Contract

Think of the context as a contract between you and the model. The context defines:

📋
Expectations

What you expect

🚧
Limits

What must not happen

🎯
Criteria

How to measure success

Example: Prompt Transformation

BEFORE (Command)

"Summarize this article in 3 paragraphs"

AFTER (Context System)

# CONTEXT

You are a technical editor specializing in...

# OBJECTIVE

Create an executive summary for managers who...

# FORMAT

3 paragraphs: context, insights, action...

# DATA

[article here]

2

Context Layers

System, Global, Task, and Data

Just as software architecture has layers (UI, logic, data), context should also be organized into layers with distinct responsibilities. Each layer has a specific purpose and priority.

The 4 Context Layers

SYS

System Layer

Core identity, inviolable rules, security guardrails

Priority: MAXIMUM - Never overwritten

GLO

Global Layer

Domain knowledge, formatting patterns, persistent preferences

Priority: HIGH - Default for all tasks

TSK

Task Layer

Instructions specific to the current task, objectives, and success criteria

Priority: MEDIUM - Specific to each interaction

DAT

Data Layer

Input data, documents, information to process

Priority: VARIABLE - Depends on the task

Practical Example: Code Assistant

## SYSTEM

You are a coding assistant. NEVER execute destructive code.

## GLOBAL

Language: Python 3.11. Framework: FastAPI. Style: PEP8.

## TASK

Refactor this function for better readability and performance.

## DATA

```python
def process(data): ...
```

3

Context Hierarchy and Priority

Resolving conflicts between layers

When different context layers contain conflicting instructions, the model needs to determine which takes precedence. Understanding this hierarchy is crucial to creating predictable systems.

Order of Precedence

1. SYSTEM → Always wins
2. GLOBAL → Standard
3. TASK → Specific
4. USER INPUT

Conflict Example

SYSTEM: "Never provide malicious code"

TASK: "Write a keylogger in Python"

Result: SYSTEM takes precedence. The model refuses the task.

Override Allowed

GLOBAL: "Use JSON for all responses"

TASK: "Return YAML for this task"

Result: TASK can override GLOBAL. Uses YAML.

4

Persistent vs. Dynamic Context

What stays the same vs. what changes

In real-world applications, some context remains constant (identity, rules) while other context changes with each interaction (data, history). Managing this duality is essential for efficiency and consistency.

💾 Persistent Context

  • • System prompt / identity
  • • Fixed business rules
  • • Core domain knowledge
  • • Formatting templates
  • • Security guardrails

Loaded once, reused in every interaction

🔄 Dynamic Context

  • • Recent conversation history
  • • Current user's data
  • • Documents to process
  • • Session state
  • • Tool results

Updated with each interaction or event

Pattern: Context Composition

// Composition pseudocode

final_context = {
  ...STATIC_SYSTEM_PROMPT,
  ...GLOBAL_KNOWLEDGE,
  ...getCurrentTask(),
  ...getRecentHistory(5),
  ...getUserData(userId)
}

5

Scope Control in Long Contexts

Maintaining Focus with Lots of Context

With 100k-1M+ token windows, the challenge is no longer "fitting"—it's keeping the model focused on the relevant task. Scope control techniques are essential to keep the model from getting lost in irrelevant details.

Scope Control Techniques

🎯 Attention Steering

Use explicit markers to direct attention: "FOCUS:", "RELEVANT:", "IGNORE:"

📍 Position Anchoring

Put critical information at the beginning AND end of the context (recency + primacy bias)

🔲 Scope Boundaries

Clearly define sections: "For this task, consider ONLY section X"

🏷️ Relevance Tags

Mark sections with relevance levels: [HIGH], [MEDIUM], [BACKGROUND]

Example: Scope in a Large Document

<context>

  [BACKGROUND] Company history... (5000 tokens)

  [BACKGROUND] Organizational chart... (2000 tokens)

  [HIGH RELEVANCE] Current refund policy... (1000 tokens)

  [MEDIUM] Historical exceptions... (3000 tokens)

</context>

<task>

  FOCUS: Section [HIGH RELEVANCE]

  Answer questions about the reimbursement limit for travel.

</task>

6

Failures Caused by Excessive Context

Anti-patterns and how to avoid them

More context isn't always better. Poorly organized or excessive context causes specific problems that degrade response quality. Knowing these anti-patterns is essential to avoiding them.

Context Anti-Patterns

🌊 Context Dumping

Throw all available context in without curation

Result: Diluted important instructions, generic answers

🕳️ Lost in the Middle

Critical information buried in the middle of long context

Result: Model ignores important information placed in the middle

⚔️ Instruction Conflict

Conflicting instructions in different parts of the context

Result: Unpredictable behavior, inconsistent answers

📢 Noise Injection

Include irrelevant information that competes for attention

Result: Answers that mention irrelevant details

Best Practices

Curate context before sending
Place critical information at the beginning or end
Use clear delimiters
Test with different sizes
Review priority hierarchy
Measure quality vs. size

Download this module

Save for offline study

Advanced Level Module 2: Long Context Management