Skip to content

Kiro CLI 提供 Agent Resources、Skills、Session Context 和 Knowledge Bases 四种上下文方式。小而稳定的项目规则适合 resources,临时文件适合 /context add,大型代码库和文档集适合 knowledge base,长对话则依靠 compaction 控制窗口。

Kiro CLI Context:Agent Resources、Skills、Session Context 和 Knowledge Bases

Kiro CLI 的效果很大程度取决于上下文管理。上下文给少了,agent 不理解项目;上下文给多了,又会浪费 tokens,甚至挤掉关键对话内容。

Kiro 提供四种主要上下文方式:

方式对上下文窗口影响是否持久适合场景
Agent Resources一直占用 tokens跨 session 持久关键项目文件、标准、配置
Skills按需加载跨 session 持久大型指南、参考文档、专业知识
Session Context一直占用 tokens仅当前 session临时文件、快速实验
Knowledge Bases只有搜索时占用跨 session 持久大代码库、大文档集

怎么选择上下文方式

简单判断:

  • 内容超过 10MB 或包含大量文件:优先 Knowledge Bases。
  • 每次对话都需要:Agent Resources。
  • 只在当前任务临时需要:Session Context。
  • 大型说明或领域知识:Skills。

例如 README、项目标准和测试规则适合 Agent Resources;临时要分析的文件适合 /context add;整个大型源码目录更适合 knowledge base。

上下文窗口影响

Kiro 会自动把工作目录和操作系统作为上下文的一部分,不需要你配置。

Context files 和 agent resources 会在每次请求中消耗 context window,即使你没有显式引用它们。Kiro 会限制 context files 最多占用模型 context window 的 75%,超出限制的文件会自动丢弃。

用下面命令查看当前上下文:

text
/context show

输出会显示 context files、tools、Kiro responses、your prompts 等占用比例。

用 agent resources 配置持久上下文

推荐通过 agent 配置文件的 resources 字段配置持久上下文:

json
{
  "name": "my-agent",
  "description": "My development agent",
  "resources": [
    "file://README.md",
    "file://docs/**/*.md",
    "file://src/config.py"
  ]
}

资源类型包括:

  • file://:启动时直接加载文件。
  • skill://:启动时加载 skill metadata,完整内容按需加载。
  • knowledgeBase:按需语义搜索,不是 URI 字符串。

如果你每次都手动 /context add README.md,说明它更适合放进 resources。

用 /context 管理临时上下文

当前 session 临时加入文件:

text
/context add README.md
/context add docs/*.md

移除文件:

text
/context remove src/temp-file.py

清空 session context:

text
/context clear

这些修改只对当前 session 有效。要永久生效,应改 agent resources。

Knowledge Bases 适合大数据集

大型代码库、文档集或参考材料不适合直接塞进 context window。可以启用 knowledge base:

bash
kiro-cli settings chat.enableKnowledge true

添加内容:

text
/knowledge add /path/to/large-codebase --include "/*.py" --exclude "node_modules/"

Knowledge bases 会在需要时被 Kiro 搜索,不会一直占用上下文窗口。

Conversation compaction

长对话会占满 context window。Kiro 支持 compaction,把旧消息摘要后释放上下文空间。

  • 手动:运行 /compact
  • 自动:context window overflow 时触发。

相关配置:

Setting默认值说明
compaction.excludeMessages2至少保留的消息对数
compaction.excludeContextWindowPercent2至少保留的上下文窗口百分比

两个设置会取更保守的值。Compaction 会创建新 session,可以用 /chat resume 回到原 session。

安全建议

上下文可能被发送给模型或用于工具调用,因此要谨慎:

  • 不要把 secrets、tokens、客户数据放进 context files。
  • .gitignore 避免敏感上下文被提交。
  • 定期审查 context files 是否过时。
  • 用具体 glob,避免把不必要的大目录加入上下文。
  • 大型资料优先用 knowledge base,减少常驻 token 消耗。

常见问题

Q: Agent Resources 和 Session Context 最大区别是什么?

A: Agent Resources 持久、每次 session 都可用;Session Context 只对当前 session 临时生效。

Q: 大代码库应该用 /context add 全部文件吗?

A: 不建议。大型代码库更适合 Knowledge Bases,避免长期占用 context window。

Q: /context remove 能移除 agent resources 吗?

A: 不能。/context 只能管理 session context,永久移除需要编辑 agent 的 resources 字段。