create-atomic-context-provider
它能做什么
它介绍如何编写一个上下文提供者,在每次运行时向智能体的系统提示词中添加一个带标题的内容块。适用于希望智能体了解当前或检索到的信息的开发者。
适用场景
- ✓当开发者希望智能体在每次调用时都知道当前时间或用户身份时。
- ✓当开发者希望将检索到的文档注入智能体的提示词时。
- ✓当两个智能体需要共享相同的会话信息时。
技能链接
https://github.com/Eigenwise/atomic-agents/blob/main/claude-plugin/atomic-agents/skills/create-atomic-context-provider/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请添加一个上下文提供者,让我的智能体获知当前用户的姓名及其保存的偏好。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能先询问数据来源以及数据需要多新鲜,然后编写一个提供者类,它返回一个带标题的内容块,并将其注册到智能体上。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Create an Atomic Agents Context Provider
A context provider injects a named, titled block into the agent's system prompt at every run(). The base prompt stays static; the context is what changes between calls.
For deep material (caching strategies, async data sources, multi-agent sharing patterns), the authority is ../framework/references/context-providers.md. This skill is the action-oriented path: clarify → write → register.
When this fires vs the umbrella framework skill
- This skill: the user wants to add a provider — "inject the user's name", "make the agent see the current time", "feed retrieved docs into the prompt", "share session state across two agents".
frameworkskill: questions about Atomic Agents in general, or about something other than authoring a provider.
Phase 1 — Clarify
Bundle into one message:
- What goes into the prompt? One sentence. Defines the provider's job.
- Where does the data come from? In-memory state mutated by your code? A vector DB lookup? A REST call? A clock?
- How fresh must it be? Per-
run()(default), every N seconds (cache), or refreshed externally before each call (async data). - Which agent(s)? One agent, or shared across multiple agents?
Skip what's already obvious from context.
Phase 2 — Plan
Confirm in one short block:
- File:
<project>/context_providers.py(or alongside the agent that owns it). - Class:
<Topic>Ctx(BaseDynamicContextProvider). - Title (rendered as the section header in the prompt) — must be unique across providers on the same agent.
- Update mechanism: mutate fields between
run()calls (most common), set via a method, orawait refresh()for async sources.
Phase 3 — Implement
Skeleton
from atomic_agents.context import BaseDynamicContextProvider
class UserCtx(BaseDynamicContextProvider):
def __init__(self):
super().__init__(title="User Context")
self.name: str = ""
self.role: str = ""
def get_info(self) -> str:
if not self.name:
return "No user is logged in."
return f"User: {self.name} (role: {self.role})"
get_info() is synchronous and runs on every agent.run() — keep it cheap. No HTTP, no DB queries, no file I/O. Cache slow sources (see "Cached" pattern below). For async data sources, await provider.refresh() from your loop before calling the agent.
Common patterns
Time — read-only, no state mutation needed:
from datetime import datetime, timezone
class TimeCtx(BaseDynamicContextProvider):
def __init__(self):
super().__init__(title="Current Time")
def get_info(self) -> str:
return datetime.now(timezone.utc).isoformat()
RAG / retrieved docs — set externally, read inside get_info():
class RAGCtx(BaseDynamicContextProvider):
def __init__(self):
super().__init__(title="Retrieved Documents")
self.docs: list[dict] = []
def set(self, docs: list[dict]) -> None:
self.docs = docs
def get_info(self) -> str:
if not self.docs:
return "No relevant documents retrieved."
return "\n\n".join(f"[{d['source']}] {d['content']}" for d in self.docs)
# In the calling code, just before agent.run():
rag.set(vector_db.search(query, k=4))
agent.run(query_input)
Session — mutable key/value state shared across agents:
class SessionCtx(BaseDynamicContextProvider):
def __init__(self):
super().__init__(title="Session")
self._data: dict[str, str] = {}
def set(self, key: str, value: str) -> None:
self._data[key] = value
def get_info(self) -> str:
if not self._data:
return "No session state."
return "\n".join(f"- {k}: {v}" for k, v in self._data.items())
Cached — for slow sources (DB schema, expensive computation):
import time
class DBSchemaCtx(BaseDynamicContextProvider):
def __init__(self, conn, ttl_seconds: int = 300):
super().__init__(title="Database Schema")
self._conn = conn
self._ttl = ttl_seconds
self._cached: str = ""
self._at: float = 0.0
def get_info(self) -> str:
now = time.time()
if not self._cached or now - self._at > self._ttl:
self._cached = render_schema(self._conn)
self._at = now
return self._cached
Async source — refresh outside, read sync inside:
clas
常见问题
什么是上下文提供者?
它是一段代码,在智能体运行时向其系统提示词中添加一个具名部分。
它会改变基础提示词吗?
不会。基础提示词保持不变,只有注入的上下文会在每次调用之间变化。
一个提供者能服务多个智能体吗?
可以。该技能涵盖了在多个智能体之间共享提供者的方法。
详情
作者的原始描述
Build a `BaseDynamicContextProvider` that injects a named, titled block into an agent's system prompt at every `run()` — current time, user identity, retrieved RAG docs, session state, cached DB schema. Use when the user asks to "add a context provider", "inject X into the prompt", "give the agent dynamic context", "wire up RAG", "make a `BaseDynamicContextProvider`", or runs `/atomic-agents:create-atomic-context-provider`.
repo: Eigenwise/atomic-agents
热度
- GitHub 星标
- 6.3k
- 排名
- #14238
- 许可证
- MIT
- 更新时间
- 2026/10/29
评论
- …
同一仓库的更多技能
create-atomic-agent
6.3k帮你的智能体构建一个新的 Atomic Agents 智能体,包括带类型的输入和输出、系统提示词,以及所选的 AI 服务商。
create-atomic-schema
6.3k帮你的智能体为 Atomic Agents 智能体和工具设计带类型的输入与输出结构定义,并附上清晰的字段说明。
create-atomic-tool
6.3k帮你的智能体为 Atomic Agents 应用构建一个带类型的工具,例如智能体可以调用的 API 封装或计算器。
framework
6.3k帮你的智能体正确使用 Atomic Agents Python 框架,涵盖结构定义、智能体、工具、提示词和服务商设置。