概述
本指南演示如何使用 Deep Agents 从零开始构建一个内容编写智能代理。 你构建的代理将能够:- 从
AGENTS.md和技能文件夹中加载语调和工作流规则 - 将网络研究任务委托给配备
web_search工具的专业子代理 - 根据加载的技能,起草博客或社交媒体内容
- 使用 Gemini 生成封面或社交媒体图片,并将文件保存在项目目录下
关键概念
本教程涵盖:先决条件
API 密钥:- Anthropic (Claude) 或其他提供商的 API 密钥
- Google (Gemini) 用于通过
gemini-2.5-flash-image生成图像 - Tavily 用于网络搜索(免费套餐)
- LangSmith 用于追踪(可选)
设置
1
创建项目目录
mkdir content-builder-agent
cd content-builder-agent
2
安装依赖项
pip install deepagents google-genai pillow pyyaml rich tavily-python langchain
uv init
uv add deepagents google-genai pillow pyyaml rich tavily-python langchain
uv sync
deepagents 固定到一个支持的版本范围(例如 >=0.3.5,<0.4.0),以匹配上游示例。3
设置 API 密钥
export ANTHROPIC_API_KEY="your_anthropic_api_key"
export GOOGLE_API_KEY="your_google_api_key"
export TAVILY_API_KEY="your_tavily_api_key" # Optional
export LANGSMITH_API_KEY="your_langsmith_api_key" # Optional
添加配置文件
示例将行为保存在三类文件中:记忆、技能和子代理定义。1
添加 AGENTS.md
在项目根目录创建 要使此代理符合你自己的语调、内容支柱和格式规则,请更新
AGENTS.md。
当你稍后创建代理并将此文件指定为记忆参数的一部分时,它会被加载到系统提示词中,从而使品牌语调和研究要求适用于每次运行。# Content Writer Agent
You are a content writer for a technology company. Your job is to create engaging, informative content that educates readers about AI, software development, and emerging technologies.
## Brand Voice
- **Professional but approachable**: Write like a knowledgeable colleague, not a textbook
- **Clear and direct**: Avoid jargon unless necessary; explain technical concepts simply
- **Confident but not arrogant**: Share expertise without being condescending
- **Engaging**: Use concrete examples, analogies, and stories to illustrate points
## Writing Standards
1. **Use active voice**: "The agent processes requests" not "Requests are processed by the agent"
2. **Lead with value**: Start with what matters to the reader, not background
3. **One idea per paragraph**: Keep paragraphs focused and scannable
4. **Concrete over abstract**: Use specific examples, numbers, and case studies
5. **End with action**: Every piece should leave the reader knowing what to do next
## Content Pillars
Our content focuses on:
- AI agents and automation
- Developer tools and productivity
- Software architecture and best practices
- Emerging technologies and trends
## Formatting Guidelines
- Use headers (H2, H3) to break up long content
- Include code examples where relevant (with syntax highlighting)
- Add bullet points for lists of 3+ items
- Keep sentences under 25 words when possible
- Include a clear call-to-action at the end
## Research Requirements
Before writing on any topic:
1. Use the `researcher` subagent for in-depth topic research
2. Gather at least 3 credible sources
3. Identify the key points readers need to understand
4. Find concrete examples or case studies to illustrate concepts
AGENTS.md 中的文本。2
添加 subagents.yaml
创建一个名为 该文件稍后在创建深度代理时将作为参数传递。
subagents.yaml 的文件。
然后添加以下文本,描述了一个 researcher 子代理,它拥有一个基于 Tavily 的 web_search 工具、一个 Haiku 模型 ID,以及将研究结果保存到你在从主代理委托任务时指定路径的指示:# Subagent definitions
# These are loaded by content_writer.py and wired up with tools
researcher:
description: >
ALWAYS use this first to research any topic before writing content.
Searches the web for current information, statistics, and sources.
When delegating, tell it the topic AND the file path to save results
(e.g., 'Research renewable energy and save to research/renewable-energy.md').
model: anthropic:claude-haiku-4-5-20251001
system_prompt: |
You are a research assistant. You have access to web_search and write_file tools.
## Your Tools
- web_search(query, max_results=5, topic="general") - Search the web
- write_file(file_path, content) - Save your findings
## Your Process
1. Use web_search to find information on the topic
2. Make 2-3 targeted searches with specific queries
3. Gather key statistics, quotes, and examples
4. Save findings to the file path specified in your task
## Important
- The user will tell you WHERE to save the file - use that exact path
- Always include source URLs in your findings
- Keep findings concise but informative
tools:
- web_search
3
添加技能
创建一个 接下来,创建 这些指示要求代理首先调用
skills/ 目录。每个技能是一个文件夹,其中包含一个 SKILL.md 文件,该文件带有 YAML 前置信息(name、description)和技能说明。创建 skills/blog-post/SKILL.md 并将以下文本复制进去,其中包含关于创建长文帖子、为 SEO 优化内容以及生成封面图片的信息。---
name: blog-post
description: Writes and structures long-form blog posts, creates tutorial outlines, and optimizes content for SEO with cover image generation. Use when the user asks to write a blog post, article, how-to guide, tutorial, technical writeup, thought leadership piece, or long-form content.
---
# Blog Post Writing Skill
## Research First (Required)
**Before writing any blog post, you MUST delegate research:**
1. Use the `task` tool with `subagent_type: "researcher"`
2. In the description, specify BOTH the topic AND where to save:
```
task(
subagent_type="researcher",
description="Research [TOPIC]. Save findings to research/[slug].md"
)
```
Example:
```
task(
subagent_type="researcher",
description="Research the current state of AI agents in 2025. Save findings to research/ai-agents-2025.md"
)
```
3. After research completes, read the findings file before writing
## Output Structure (Required)
**Every blog post MUST have both a post AND a cover image:**
```
blogs/
└── <slug>/
├── post.md # The blog post content
└── hero.png # REQUIRED: Generated cover image
```
Example: A post about "AI Agents in 2025" → `blogs/ai-agents-2025/`
**You MUST complete both steps:**
1. Write the post to `blogs/<slug>/post.md`
2. Generate a cover image using `generate_image` and save to `blogs/<slug>/hero.png`
**A blog post is NOT complete without its cover image.**
## Blog Post Structure
Every blog post should follow this structure:
### 1. Hook (Opening)
- Start with a compelling question, statistic, or statement
- Make the reader want to continue
- Keep it to 2-3 sentences
### 2. Context (The Problem)
- Explain why this topic matters
- Describe the problem or opportunity
- Connect to the reader's experience
### 3. Main Content (The Solution)
- Break into 3-5 main sections with H2 headers
- Each section covers one key point
- Include code examples, diagrams, or screenshots where helpful
- Use bullet points for lists
### 4. Practical Application
- Show how to apply the concepts
- Include step-by-step instructions if applicable
- Provide code snippets or templates
### 5. Conclusion & CTA
- Summarize key takeaways (3 bullets max)
- End with a clear call-to-action
- Link to related resources
## Cover Image Generation
After writing the post, generate a cover image using the `generate_cover` tool:
```
generate_cover(prompt="A detailed description of the image...", slug="your-blog-slug")
```
The tool saves the image to `blogs/<slug>/hero.png`.
### Writing Effective Image Prompts
Structure your prompt with these elements:
1. **Subject**: What is the main focus? Be specific and concrete.
2. **Style**: Art direction (minimalist, isometric, flat design, 3D render, watercolor, etc.)
3. **Composition**: How elements are arranged (centered, rule of thirds, symmetrical)
4. **Color palette**: Specific colors or mood (warm earth tones, cool blues and purples, high contrast)
5. **Lighting/Atmosphere**: Soft diffused light, dramatic shadows, golden hour, neon glow
6. **Technical details**: Aspect ratio considerations, negative space for text overlay
### Example Prompts
**For a technical blog post:**
```
Isometric 3D illustration of interconnected glowing cubes representing AI agents, each cube has subtle circuit patterns. Cubes connected by luminous data streams. Deep navy background (#0a192f) with electric blue (#64ffda) and soft purple (#c792ea) accents. Clean minimal style, lots of negative space at top for title. Professional tech aesthetic.
```
**For a tutorial/how-to:**
```
Clean flat illustration of hands typing on a keyboard with abstract code symbols floating upward, transforming into lightbulbs and gears. Warm gradient background from soft coral to light peach. Friendly, approachable style. Centered composition with space for text overlay.
```
**For thought leadership:**
```
Abstract visualization of a human silhouette profile merging with geometric neural network patterns. Split composition - organic watercolor texture on left transitioning to clean vector lines on right. Muted sage green and warm terracotta color scheme. Contemplative, forward-thinking mood.
```
## SEO Considerations
- Include the main keyword in the title and first paragraph
- Use the keyword naturally 3-5 times throughout
- Keep the title under 60 characters
- Write a meta description (150-160 characters)
## Quality Checklist
Before finishing:
- [ ] Post saved to `blogs/<slug>/post.md`
- [ ] Hero image generated at `blogs/<slug>/hero.png`
- [ ] Hook grabs attention in first 2 sentences
- [ ] Each section has a clear purpose
- [ ] Conclusion summarizes key points
- [ ] CTA tells reader what to do next
skills/social-media/SKILL.md 并将以下文本复制进去,其中包含关于起草社交媒体帖子和生成配图的信息:---
name: social-media
description: Drafts engaging social media posts, writes hooks, suggests hashtags, creates thread structures, and generates companion images. Use when the user asks to write a LinkedIn post, tweet, Twitter/X thread, social media caption, social post, or repurpose content for social platforms.
---
# Social Media Content Skill
## Research First (Required)
**Before writing any social media content, you MUST delegate research:**
1. Use the `task` tool with `subagent_type: "researcher"`
2. In the description, specify BOTH the topic AND where to save:
```
task(
subagent_type="researcher",
description="Research [TOPIC]. Save findings to research/[slug].md"
)
```
Example:
```
task(
subagent_type="researcher",
description="Research renewable energy trends in 2025. Save findings to research/renewable-energy.md"
)
```
3. After research completes, read the findings file before writing
## Output Structure (Required)
**Every social media post MUST have both content AND an image:**
**LinkedIn posts:**
```
linkedin/
└── <slug>/
├── post.md # The post content
└── image.png # REQUIRED: Generated visual
```
**Twitter/X threads:**
```
tweets/
└── <slug>/
├── thread.md # The thread content
└── image.png # REQUIRED: Generated visual
```
Example: A LinkedIn post about "prompt engineering" → `linkedin/prompt-engineering/`
**You MUST complete both steps:**
1. Write the content to the appropriate path
2. Generate an image using `generate_image` and save alongside the post
**A social media post is NOT complete without its image.**
## Platform Guidelines
### LinkedIn
**Format:**
- 1,300 character limit (show more after ~210 chars)
- First line is crucial - make it hook
- Use line breaks for readability
- 3-5 hashtags at the end
**Tone:**
- Professional but personal
- Share insights and learnings
- Ask questions to drive engagement
- Use "I" and share experiences
**Structure:**
```
[Hook - 1 compelling line]
[Empty line]
[Context - why this matters]
[Empty line]
[Main insight - 2-3 short paragraphs]
[Empty line]
[Call to action or question]
#hashtag1 #hashtag2 #hashtag3
```
### Twitter/X
**Format:**
- 280 character limit per tweet
- Threads for longer content (use 1/🧵 format)
- No more than 2 hashtags per tweet
**Thread Structure:**
```
1/🧵 [Hook - the main insight]
2/ [Supporting point 1]
3/ [Supporting point 2]
4/ [Example or evidence]
5/ [Conclusion + CTA]
```
## Image Generation
Every social media post needs an eye-catching image. Use the `generate_social_image` tool:
```
generate_social_image(prompt="A detailed description...", platform="linkedin", slug="your-post-slug")
```
The tool saves the image to `<platform>/<slug>/image.png`.
### Social Image Best Practices
Social images need to work at small sizes in crowded feeds:
- **Bold, simple compositions** - one clear focal point
- **High contrast** - stands out when scrolling
- **No text in image** - too small to read, platforms add their own
- **Square or 4:5 ratio** - works across platforms
### Writing Effective Prompts
Include these elements:
1. **Single focal point**: One clear subject, not a busy scene
2. **Bold style**: Vibrant colors, strong shapes, high contrast
3. **Simple background**: Solid color, gradient, or subtle texture
4. **Mood/energy**: Match the post tone (inspiring, urgent, thoughtful)
### Example Prompts
**For an insight/tip post:**
```
Single glowing lightbulb floating against a deep purple gradient background, lightbulb made of interconnected golden geometric lines, rays of soft light emanating outward. Minimal, striking, high contrast. Square composition.
```
**For announcements/news:**
```
Abstract rocket ship made of colorful geometric shapes launching upward with a trail of particles. Bright coral and teal color scheme against clean white background. Energetic, celebratory mood. Bold flat illustration style.
```
**For thought-provoking content:**
```
Two overlapping translucent circles, one blue one orange, creating a glowing intersection in the center. Represents collaboration or intersection of ideas. Dark charcoal background, soft ethereal glow. Minimalist and contemplative.
```
## Content Types
### Announcement Posts
- Lead with the news
- Explain the impact
- Include link or next step
### Insight Posts
- Share one specific learning
- Explain the context briefly
- Make it actionable
### Question Posts
- Ask a genuine question
- Provide your take first
- Keep it focused on one topic
## Quality Checklist
Before finishing:
- [ ] Post saved to `linkedin/<slug>/post.md` or `tweets/<slug>/thread.md`
- [ ] Image generated alongside the post
- [ ] First line hooks attention
- [ ] Content fits platform limits
- [ ] Tone matches platform norms
- [ ] Has clear CTA or question
- [ ] Hashtags are relevant (not generic)
researcher 子代理,在 blogs/、linkedin/ 或 tweets/ 下写入 markdown,并为图片调用 generate_cover 或 generate_social_image。当你稍后创建代理并指定技能文件夹时,这些技能文件夹中 SKILLS.md 文件的前置信息将被加载到系统提示词中,以便当任务匹配技能描述时,代理可以使用该技能。构建脚本
在项目根目录创建content_writer.py。以下部分应按顺序放在同一个文件中。
1
添加工具
researcher 子代理使用 Tavily 搜索。
博客和社交工作流使用 Gemini 图像生成。
稍后创建代理时,
load_subagents 函数会读取 subagents.yaml 并将工具名称解析为这些装饰过的函数。import os
from pathlib import Path
from typing import Literal
import yaml
from langchain.tools import tool
EXAMPLE_DIR = Path(__file__).parent
@tool
def web_search(
query: str,
max_results: int = 5,
topic: Literal["general", "news"] = "general",
) -> dict:
"""Search the web for current information.
Args:
query: The search query (be specific and detailed)
max_results: Number of results to return (default: 5)
topic: "general" for most queries, "news" for current events
Returns:
Search results with titles, URLs, and content excerpts.
"""
try:
from tavily import TavilyClient
api_key = os.environ.get("TAVILY_API_KEY")
if not api_key:
return {"error": "TAVILY_API_KEY not set"}
client = TavilyClient(api_key=api_key)
return client.search(query, max_results=max_results, topic=topic)
except Exception as e:
return {"error": f"Search failed: {e}"}
@tool
def generate_cover(prompt: str, slug: str) -> str:
"""Generate a cover image for a blog post.
Args:
prompt: Detailed description of the image to generate.
slug: Blog post slug. Image saves to blogs/<slug>/hero.png
"""
try:
from google import genai
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash-image",
contents=[prompt],
)
for part in response.parts:
if part.inline_data is not None:
image = part.as_image()
output_path = EXAMPLE_DIR / "blogs" / slug / "hero.png"
output_path.parent.mkdir(parents=True, exist_ok=True)
image.save(str(output_path))
return f"Image saved to {output_path}"
return "No image generated"
except Exception as e:
return f"Error: {e}"
@tool
def generate_social_image(prompt: str, platform: str, slug: str) -> str:
"""Generate an image for a social media post.
Args:
prompt: Detailed description of the image to generate.
platform: Either "linkedin" or "tweets"
slug: Post slug. Image saves to <platform>/<slug>/image.png
"""
try:
from google import genai
client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash-image",
contents=[prompt],
)
for part in response.parts:
if part.inline_data is not None:
image = part.as_image()
output_path = EXAMPLE_DIR / platform / slug / "image.png"
output_path.parent.mkdir(parents=True, exist_ok=True)
image.save(str(output_path))
return f"Image saved to {output_path}"
return "No image generated"
except Exception as e:
return f"Error: {e}"
def load_subagents(config_path: Path) -> list:
"""Load subagent definitions from YAML and wire up tools.
Unlike `memory` and `skills`, deep agents do not load subagents from files by default.
This helper externalizes configuration so you can edit YAML without changing Python code.
"""
available_tools = {
"web_search": web_search,
}
with open(config_path) as f:
config = yaml.safe_load(f)
subagents = []
for name, spec in config.items():
subagent = {
"name": name,
"description": spec["description"],
"system_prompt": spec["system_prompt"],
}
if "model" in spec:
subagent["model"] = spec["model"]
if "tools" in spec:
subagent["tools"] = [available_tools[t] for t in spec["tools"]]
subagents.append(subagent)
return subagents
2
创建代理
使用 create_deep_agent 创建深度代理时,传递记忆路径、技能目录、图像工具、来自 YAML 的子代理,以及一个以示例目录为根目录的 FilesystemBackend,以便
./AGENTS.md 和 ./skills/ 等路径能正确解析。from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend
def create_content_writer():
"""创建一个通过文件系统文件配置的内容写作代理。"""
return create_deep_agent(
model="google_genai:gemini-3.6-flash",
memory=["./AGENTS.md"],
skills=["./skills/"],
tools=[generate_cover, generate_social_image],
subagents=load_subagents(EXAMPLE_DIR / "subagents.yaml"),
backend=FilesystemBackend(root_dir=EXAMPLE_DIR),
)
from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend
def create_content_writer():
"""创建一个通过文件系统文件配置的内容写作代理。"""
return create_deep_agent(
model="openai:gpt-5.5",
memory=["./AGENTS.md"],
skills=["./skills/"],
tools=[generate_cover, generate_social_image],
subagents=load_subagents(EXAMPLE_DIR / "subagents.yaml"),
backend=FilesystemBackend(root_dir=EXAMPLE_DIR),
)
from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend
def create_content_writer():
"""创建一个通过文件系统文件配置的内容写作代理。"""
return create_deep_agent(
model="anthropic:claude-sonnet-4-6",
memory=["./AGENTS.md"],
skills=["./skills/"],
tools=[generate_cover, generate_social_image],
subagents=load_subagents(EXAMPLE_DIR / "subagents.yaml"),
backend=FilesystemBackend(root_dir=EXAMPLE_DIR),
)
from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend
def create_content_writer():
"""创建一个通过文件系统文件配置的内容写作代理。"""
return create_deep_agent(
model="openrouter:z-ai/glm-5.2",
memory=["./AGENTS.md"],
skills=["./skills/"],
tools=[generate_cover, generate_social_image],
subagents=load_subagents(EXAMPLE_DIR / "subagents.yaml"),
backend=FilesystemBackend(root_dir=EXAMPLE_DIR),
)
from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend
def create_content_writer():
"""创建一个通过文件系统文件配置的内容写作代理。"""
return create_deep_agent(
model="fireworks:accounts/fireworks/models/glm-5p2",
memory=["./AGENTS.md"],
skills=["./skills/"],
tools=[generate_cover, generate_social_image],
subagents=load_subagents(EXAMPLE_DIR / "subagents.yaml"),
backend=FilesystemBackend(root_dir=EXAMPLE_DIR),
)
from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend
def create_content_writer():
"""创建一个通过文件系统文件配置的内容写作代理。"""
return create_deep_agent(
model="baseten:zai-org/GLM-5.2",
memory=["./AGENTS.md"],
skills=["./skills/"],
tools=[generate_cover, generate_social_image],
subagents=load_subagents(EXAMPLE_DIR / "subagents.yaml"),
backend=FilesystemBackend(root_dir=EXAMPLE_DIR),
)
from deepagents import create_deep_agent
from deepagents.backends import FilesystemBackend
def create_content_writer():
"""创建一个通过文件系统文件配置的内容写作代理。"""
return create_deep_agent(
model="ollama:north-mini-code-1.0",
memory=["./AGENTS.md"],
skills=["./skills/"],
tools=[generate_cover, generate_social_image],
subagents=load_subagents(EXAMPLE_DIR / "subagents.yaml"),
backend=FilesystemBackend(root_dir=EXAMPLE_DIR),
)
3
添加入口点
使用用户消息调用代理以验证代理是否正常工作:
import sys
from langchain.messages import HumanMessage
if __name__ == "__main__":
task = (
" ".join(sys.argv[1:])
if len(sys.argv) > 1
else "Write a blog post about how AI agents are transforming software development"
)
agent = create_content_writer()
result = agent.invoke(
{"messages": [HumanMessage(content=task)]},
config={"configurable": {"thread_id": "content-builder-demo"}},
)
for msg in result.get("messages", []):
if hasattr(msg, "content") and msg.content:
print(msg.content)
运行代理
文件系统后端可以读取、写入和删除
root_dir 下的文件。请仅在专用目录中运行,并在发布前审查生成的内容。python content_writer.py
python content_writer.py Write a blog post about prompt engineering
LANGSMITH_API_KEY 后,你可以在 LangSmith 中检查运行记录。
输出
成功时,生成的文件会写入系统临时目录(在 macOS 和 Linux 上,通常位于/tmp/),而不是项目文件旁边。
blogs/
└── prompt-engineering/
├── post.md
└── hero.png
research/
└── prompt-engineering.md
SKILL.md 中的技能说明。
完整代码
在 GitHub 上浏览完整的 content-builder-agent 示例,包括基于 Rich 的流式 UI。后续步骤
- 编辑
AGENTS.md以更改品牌语调和研究要求 - 在
skills/<name>/SKILL.md下添加技能以支持新的内容类型 - 在
subagents.yaml中添加子代理,并在load_subagents中注册工具 - 阅读 子代理、技能 和 自定义 以进行更深入的配置
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

