Communitygithub.com

上下文工程

检查上下文使用限制,监控剩余时间,优化令牌消耗,调试上下文故障。适用于查询上下文百分比、速率限制、使用警告、上下文优化、代理架构和内存系统。

上下文工程 是什么?

上下文工程技能旨在帮助AI代理系统最大化推理质量并最小化令牌消耗。它提供了一套核心原则、策略和指标,用于设计、调试和优化基于LLM的应用。该技能支持Claude Code等平台,通过实时监控上下文窗口使用情况、令牌利用率、缓存命中率和系统性能指标,帮助用户识别并解决上下文限制导致的性能下降问题。其核心策略包括四桶策略(写入、选择、压缩、隔离)以及运行时感知机制,后者通过PostToolUse钩子自动注入使用警告。用户可以利用脚本进行上下文健康分析和压缩质量评估,从而在成本、延迟和质量之间取得最佳平衡。该技能适用于构建多代理协调系统、实施记忆系统、开发LLM驱动的流水线以及评估代理性能等场景。

兼容平台✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/majiayu000/claude-skill-registry-data/tree/HEAD/context-management/context-engineering-hoanghd218-landing-page-ai-kien

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

上下文工程 是做什么的?

上下文工程致力于为LLM任务策划最小的高信噪比令牌集。目标是:在最大化推理质量的同时,最小化令牌消耗。

何时激活

  • 设计/调试代理系统时
  • 上下文限制制约性能时
  • 优化成本/延迟时
  • 构建多代理协调时
  • 实施记忆系统时
  • 评估代理性能时
  • 开发LLM驱动的流水线时

核心原则

  1. 上下文质量 > 数量 - 高信噪比令牌优于详尽内容
  2. 注意力是有限的 - U形曲线偏好开始/结束位置
  3. 渐进式披露 - 按需加载信息
  4. 隔离防止退化 - 将工作划分到子代理
  5. 先测量再优化 - 了解基线

重要提示:

  • 为了简洁,可以牺牲语法。
  • 在保证高质量的同时确保令牌效率。
  • 将这些规则传递给子代理。

快速参考

主题何时使用参考
基础理解上下文结构、注意力机制context-fundamentals.md
退化调试故障、中间丢失、中毒context-degradation.md
优化压缩、掩码、缓存、分区context-optimization.md
压缩长会话、摘要策略context-compression.md
记忆跨会话持久化、知识图谱memory-systems.md
多代理协调模式、上下文隔离multi-agent-patterns.md
评估测试代理、LLM作为评委、指标evaluation.md
工具设计工具整合、描述工程tool-design.md
流水线项目开发、批处理project-development.md
运行时感知使用限制、上下文窗口监控runtime-awareness.md

关键指标

  • 令牌利用率:70%时警告,80%时触发优化
  • 令牌方差:解释80%的代理性能差异
  • 多代理成本:约为单代理基线的15倍
  • 压缩目标:减少50-70%,质量损失<5%
  • 缓存命中目标:稳定工作负载中达到70%+

四桶策略

  1. 写入:将上下文保存到外部(暂存板、文件)
  2. 选择:仅拉取相关上下文(检索、过滤)
  3. 压缩:在保留信息的同时减少令牌(摘要)
  4. 隔离:跨子代理拆分(分区)

反模式

  • 详尽上下文优于策划上下文
  • 关键信息放在中间位置
  • 在达到限制前没有压缩触发
  • 单个代理处理可并行化任务
  • 工具没有清晰的描述

指南

  1. 将关键信息放在上下文的开始/结束位置
  2. 在利用率达到70-80%时进行压缩
  3. 使用子代理进行上下文隔离,而不是角色扮演
  4. 使用四问框架(是什么、何时用、输入是什么、返回什么)设计工具
  5. 优化每个任务的令牌数,而非每个请求的令牌数
  6. 使用基于探测的评估进行验证
  7. 在生产环境中监控KV缓存命中率
  8. 从最小化开始,仅在证明必要时增加复杂性

运行时感知

系统通过PostToolUse钩子自动注入使用感知:

<usage-awareness>
Claude使用限制:5h=45%,7d=32%
上下文窗口使用率:67%
</usage-awareness>

阈值:

  • 70%:警告 - 考虑优化/压缩
  • 90%:严重 - 需要立即操作

数据来源:

  • 使用限制:Anthropic OAuth API(https://api.anthropic.com/api/oauth/usage)
  • 上下文窗口:状态行临时文件(/tmp/ck-context-{session_id}.json)

脚本

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

majiayu000/claude-skill-registry-data

Edit existing BMAD agents while maintaining compliance

majiayu000/claude-skill-registry-data

Edit existing BMAD agents while maintaining compliance

majiayu000/claude-skill-registry-data

Edit existing BMAD modules while maintaining coherence

majiayu000/claude-skill-registry-data

Edit existing BMAD modules while maintaining coherence

majiayu000/claude-skill-registry-data

Analyzes current state and user query to answer BMad questions or recommend the next workflow or agent. Use when user says what should I do next, what do I do now, or asks a question about BMad

majiayu000/claude-skill-registry-data

Build AI agents with Google ADK Python (Agent Development Kit). Use for multi-agent systems, workflow agents (sequential/parallel/loop), Vertex AI deployment, tool integration, human-in-the-loop.

majiayu000/claude-skill-registry-data

Build AI agents with Google ADK Python (Agent Development Kit). Use for multi-agent systems, workflow agents (sequential/parallel/loop), Vertex AI deployment, tool integration, human-in-the-loop.

majiayu000/claude-skill-registry-data

Portfolio allocation and rebalancing optimizer. Manages asset allocation across stocks/cash/bonds, performs periodic rebalancing, and ensures diversification according to market regime and risk tolerance.

majiayu000/claude-skill-registry-data

Create a hilarious and ultra-realistic video of an anthropomorphic animal acting like a human vlogger in a real-world setting.

majiayu000/claude-skill-registry-data

Speech-to-text transcription and translation via OpenAI Audio API -- models, response formats, timestamps, prompting, streaming, chunking, and diarization

majiayu000/claude-skill-registry-data

This skill should be used when the user asks about libraries, frameworks, API references, or needs code examples. Activates for setup questions, code generation involving libraries, or mentions of specific frameworks like React, Vue, Next.js, Prisma, Supabase, etc.

majiayu000/claude-skill-registry-data

小省导购员数字人带货版即梦视频提示词生成系统,基于四大智能体协同(提示词生成师、质量管控师、知识库运维师、跨环节适配师),按照"主体+运动+场景+(镜头语言+光影+氛围)"公式输出中英文双版提示词,适配5s短视频。确保人物一致性、视觉连贯性、情绪连贯性,支持知识库智能复用和跨工具适配(Suno音乐、AI绘画),为数字人带货视频提供高质量提示词生成服务。

majiayu000/claude-skill-registry-data

Upload and manage files using Google Gemini File API via scripts/. Use for uploading images, audio, video, PDFs, and other files for use with Gemini models. Supports file upload, status checking, and file management. Triggers on "upload file", "file API", "upload image", "upload PDF", "upload video", "file management".

majiayu000/claude-skill-registry-data

Analyze images/audio/video with Gemini API (better vision than Claude). Generate images (Imagen 4), videos (Veo 3). Use for vision analysis, transcription, OCR, design extraction, multimodal AI.

majiayu000/claude-skill-registry-data

Analyze images/audio/video with Gemini API (better vision than Claude). Generate images (Imagen 4), videos (Veo 3). Use for vision analysis, transcription, OCR, design extraction, multimodal AI.

majiayu000/claude-skill-registry-data

Skill for discovering and researching autonomous AI agents, tools, and ecosystems using the AgentFolio directory.

majiayu000/claude-skill-registry-data

Conduct domain and industry research. Use when the user says "lets create a research report on [domain or industry]

majiayu000/claude-skill-registry-data

Conduct market research on competition and customers. Use when the user says "create a market research report about [business idea]".

majiayu000/claude-skill-registry-data

Conduct technical research on technologies and architecture. Use when the user says "create a technical research report on [topic]".

majiayu000/claude-skill-registry-data

Builds, edit or validate Agent Skill through conversational discovery. Use when the user requests to "Create an Agent", "Optimize an Agent" or "Edit an Agent".

相关技能