context-engineering

context-engineering is a skill for Claude Code, Codex from bojieli/ai-agent-book. It costs 96 tokens per session (2,644 once invoked), scanned A, original, Apache-2.0.

A guide to organizing the instructions and information an AI agent receives at each step. It covers system prompts, tool descriptions, conversation history, on-demand skills, and protections against unsafe outside content.

In plain words
What is it for?
Use it to write system prompts, structure messages and tools, create SKILL.md files, decide what to load only when needed, and reduce prompt-injection risk from webpages, documents, or email.
Why use it?
Agents often fail because required rules, code details, or environment facts are missing, buried, or presented in a confusing order. Good context design makes the right information easier for the model to use.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one. Also seen: positional $N argument.

Good fit Use it to write system prompts, structure messages and tools, create SKILL.md files, decide what to load only when needed, and reduce prompt-injection risk from webpages, documents, or email.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/bojieli/ai-agent-book/context-engineering
About the project

AI Agent: Design Principles and Engineering Practice is an open-source book that explains how AI agents combine language models, context, and tools, with accompanying experiments and code. It is intended for readers studying the principles and engineering of AI agents, from fundamentals through production use. The catalogue skills support coding-agent work related to the book's subject matter.

bojieli/ai-agent-book · 54,406 stars · on GitHub

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add bojieli/ai-agent-book --skill context-engineering
Clone the repo
git clone --depth 1 https://github.com/bojieli/ai-agent-book

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for context-engineering

README.md
[![agentmods](https://agentmods.dev/badge/skills/bojieli/ai-agent-book/context-engineering/github.svg)](https://agentmods.dev/skills/bojieli/ai-agent-book/context-engineering)
Your own site
<a href="https://agentmods.dev/skills/bojieli/ai-agent-book/context-engineering"><img src="https://agentmods.dev/badge/skills/bojieli/ai-agent-book/context-engineering/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for context-engineering

Your own site · 80×15
<a href="https://agentmods.dev/skills/bojieli/ai-agent-book/context-engineering"><img src="https://agentmods.dev/badge/skills/bojieli/ai-agent-book/context-engineering.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 96 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,644 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00096 $0.02644
Opus 5.5 $0.00038 $0.01058
Sonnet 5.5 $0.00019 $0.00529
Haiku 4.5 $0.00010 $0.00264

Measured 22d ago against content hash 3c36a03787a3, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

context-engineering scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 22d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/context-engineering/SKILL.md · 101 lines

How it starts

The opening of the file, as written. The whole thing — 101 lines — stays where its author put it; the contents beside it link to each section on GitHub.

上下文工程总纲

上下文是 Agent 在每个决策点实际「看到」的全部信息:系统提示词、工具定义、用户消息、模型回复、工具执行结果。上下文工程就是系统性设计、组织、供给这些信息——模型智力只是基础,上下文质量才是 Agent 能力的真正关键。一个中等能力的模型配上精心组织的上下文,往往胜过一个顶级模型在信息匮乏下的盲目摸索。

何时使用

  • 设计或重构 Agent 的系统提示词、工具定义、消息列表结构
  • 编写 / 审查一份 SKILL.md(元数据、路由描述、正文分层)
  • 排查「Agent 行为不稳定、违反业务规则、重复犯错、忘记约束」
  • 决定哪些知识该进系统提示词、哪些该做成按需加载的 Skill
  • 评估外部内容(网页、文档、邮件)进入上下文后的安全风险

核心原则

  • API 无状态,上下文由框架每轮重建。 模型不会「记住」上一轮,Agent 框架必须每次都把完整消息历史送回去。
  • 上下文 = 四种消息角色 + tools 字段。 system(开发者规则,通常只有一条且在最前)、user(终端用户输入)、assistant(模型回复含 tool_calls)、tool(框架执行结果,靠 tool_call_id 与调用关联);工具定义走请求顶层 tools 字段,不是消息角色。
  • 静态前缀 + 动态轨迹。 system prompt 与 tool definitions 全程不变,对话历史(trajectory)随交互持续增长。所有后续技术都在优化这个列表的内容与结构。
  • 模型决策、框架执行。 模型只发出工具调用请求,真正执行工具的是 Agent 框架;循环「请求 → tool_calls → 执行 → 送回结果 → 再请求」就是 ReAct 循环的 API 实现。
  • 最低信息需求三件套:代码信息(结构、职责、规范)、流程规范(分支、提交、评审、CI/CD)、环境信息(配置、数据库、密钥管理)。缺哪一类,Agent 就在哪一类上出错。
  • 新员工检验法:如果一个聪明的新员工读完你的系统提示词还不知道该怎么做,Agent 也一样不知道。
  • 上下文学习是检索而非推理:模型擅长从已有内容中查找,不擅长在前向传播中归纳统计。位置偏好(Lost in the Middle)意味着开头和结尾注意力最高,中部易被忽视——关键信息放两端。
  • 与模型厂商的训练方法保持一致:选择 Agent 交互模式时优先采用厂商专门训练过的模式(Skills、tool_search 等),同生态内表现最佳。

实践模式

1. 系统提示词四维度

  • 语气与风格:用明确指令而非请求,如 You MUST answer concisely with fewer than 4 lines;无法完成任务时 keep your response to 1-2 sentences 且不要解释为什么不能做。大写(NEVER do X)比 Please avoid doing X 更能引起注意,但过度使用会稀释效果,只留给真正关键的约束。
  • 结构化:Markdown 标题(#、##)提供人可读的层次,XML 标签提供机器可解析的精确语义(<file_operation>、<network_request>),标签名本身携带语义。双层配合:Markdown 管组织逻辑,XML 管语义边界。
  • 流程驱动而非规则堆砌:写成带分支的 SOP(Step 1 Validation → Step 2 Classification → ... → Step 5 Verification),让模型任何时刻都知道自己在哪个阶段、异常时按阶段处理,而不是遍历上百条零散规则找匹配项。
  • 业务规则细化到可执行:把模糊规则(「根据任务情况选择合适的计费类型」)改写成布尔判断,例如 NEVER use percentage_based_one_time for refunds and service cancellations. Use fixed_fee instead.;成功率阈值直接映射到行为(>60% 可退款、<30% 拒绝任务);计费粒度写死(每分钟 $0.05,汇总四舍五入到整数美元),并明确「节省」只基于现有账单计算。提示词由产品经理基于线上数据设计,工程师负责准确编码,不擅自决定业务逻辑。

2. few-shot 示例的两个决策点

  • 位置:放系统提示词 = 成为静态前缀对所有请求生效;或伪造一组 user/assistant 消息放在首轮对话中,适合按会话类型选用不同示例集。
  • 稳定性:无论放哪,示例都处于上下文靠前区域,一旦确定必须字节级稳定——按请求动态检索「最相关」的示例等于每轮改写前缀,缓存会持续失效。为每类任务准备固定示例集。
  • 数量:两三个精心挑选、覆盖边界情况的示例,胜过十个大同小异的示例——后者既占上下文,又稀释对规则本身的注意力。

Read the full file on GitHub · 101 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 22d ago First seen · 101 lines · 96 tokens per session scan A 3c36a03787a3

Subscribe to this mod's changes

context-engineering is a skill published in the GitHub repository bojieli/ai-agent-book (54,406 stars, last pushed 7d ago), licensed Apache-2.0. It adds 96 tokens to every session and 2,644 once invoked, about $0.0004 per session on Opus 5.5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-24.

Related

Other skills, from other repositories