agent-chain-analyzer
Detects and analyzes agent chain anti-patterns where agents invoke other agents sequentially causing massive context load
Best use case
agent-chain-analyzer is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Detects and analyzes agent chain anti-patterns where agents invoke other agents sequentially causing massive context load
Teams using agent-chain-analyzer should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/agent-chain-analyzer/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How agent-chain-analyzer Compares
| Feature / Agent | agent-chain-analyzer | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Detects and analyzes agent chain anti-patterns where agents invoke other agents sequentially causing massive context load
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# Agent Chain Analyzer Skill
<CONTEXT>
You detect and analyze the **Agent Chain anti-pattern** - one of the most severe architectural issues in pre-skills Claude Code projects.
**Agent Chain Pattern**: Agent1 invokes Agent2 via Task tool, which invokes Agent3, forming a sequential chain with massive context load.
**Impact**:
- 180K+ tokens for 4-agent chain (4 × 45K)
- No state persistence across chain
- No user approval workflow
- Fragile (single failure breaks chain)
**Solution**: Convert to Manager-as-Agent + Skills (53% context reduction)
You execute deterministic scripts to detect chains, map dependencies, calculate depth, and estimate context impact.
</CONTEXT>
<CRITICAL_RULES>
1. ALWAYS detect ALL agent chains in project (not just obvious ones)
2. ALWAYS calculate full chain depth and context impact
3. ALWAYS map complete dependency graph
4. ALWAYS return structured JSON output
5. NEVER modify project files (read-only analysis)
6. NEVER miss indirect chains (Agent1 → Agent2 → Agent3 → Agent4)
</CRITICAL_RULES>
<OPERATIONS>
## detect-agent-chains
Detect all agent chain patterns in project.
**Input:**
- `project_path`: Path to Claude Code project root
**Process:**
1. Execute: `scripts/detect-agent-chains.sh "{project_path}"`
2. Parse JSON output
3. Return chain detection results
**Output:**
```json
{
"status": "success",
"chains_detected": true,
"chain_count": 2,
"chains": [
{
"chain_id": "catalog-process-chain",
"entry_point": "catalog-fetcher",
"agents": ["catalog-fetcher", "catalog-analyzer", "catalog-validator", "catalog-reporter"],
"depth": 4,
"locations": [
".claude/agents/catalog-fetcher.md",
".claude/agents/catalog-analyzer.md",
".claude/agents/catalog-validator.md",
".claude/agents/catalog-reporter.md"
],
"invocation_pattern": "Task tool",
"context_estimate": 180000
}
],
"total_agents_in_chains": 7,
"max_chain_depth": 4
}
```
---
## analyze-chain-depth
Analyze chain depth and complexity for each detected chain.
**Input:**
- `project_path`: Path to Claude Code project root
- `chains`: Detected chains from detect-agent-chains operation
**Process:**
1. Execute: `scripts/analyze-chain-depth.sh "{project_path}" "{chains_json}"`
2. Calculate depth metrics
3. Identify complexity factors
**Output:**
```json
{
"status": "success",
"depth_analysis": [
{
"chain_id": "catalog-process-chain",
"depth": 4,
"complexity_score": 0.85,
"complexity_factors": [
"Deep chain (4 agents)",
"No error handling between agents",
"No state persistence",
"Sequential only (no parallelism)"
],
"migration_complexity": "high",
"estimated_refactor_days": 15
}
],
"average_depth": 3.5,
"deepest_chain": {
"chain_id": "catalog-process-chain",
"depth": 4
}
}
```
---
## map-chain-dependencies
Map complete dependency graph for agent chains.
**Input:**
- `project_path`: Path to Claude Code project root
- `chains`: Detected chains from detect-agent-chains operation
**Process:**
1. Execute: `scripts/map-chain-dependencies.sh "{project_path}" "{chains_json}"`
2. Build dependency graph
3. Identify data flow between agents
**Output:**
```json
{
"status": "success",
"dependency_graph": {
"nodes": [
{"id": "catalog-fetcher", "type": "entry_point"},
{"id": "catalog-analyzer", "type": "intermediate"},
{"id": "catalog-validator", "type": "intermediate"},
{"id": "catalog-reporter", "type": "terminal"}
],
"edges": [
{
"from": "catalog-fetcher",
"to": "catalog-analyzer",
"data_passed": ["catalog_data", "metadata"],
"invocation_method": "Task tool"
},
{
"from": "catalog-analyzer",
"to": "catalog-validator",
"data_passed": ["analysis_results"],
"invocation_method": "Task tool"
},
{
"from": "catalog-validator",
"to": "catalog-reporter",
"data_passed": ["validation_results"],
"invocation_method": "Task tool"
}
]
},
"data_flow": [
"catalog_data (fetcher → analyzer)",
"analysis_results (analyzer → validator)",
"validation_results (validator → reporter)"
],
"parallelization_opportunities": [
"analyzer and validator could run in parallel if refactored"
]
}
```
---
## estimate-chain-context
Calculate context load impact for agent chains.
**Input:**
- `project_path`: Path to Claude Code project root
- `chains`: Detected chains from detect-agent-chains operation
**Process:**
1. Execute: `scripts/estimate-chain-context.sh "{project_path}" "{chains_json}"`
2. Calculate current context load
3. Estimate post-migration load
4. Calculate reduction percentage
**Output:**
```json
{
"status": "success",
"context_estimates": [
{
"chain_id": "catalog-process-chain",
"current_context": {
"agents": 4,
"tokens_per_agent": 45000,
"total_tokens": 180000,
"overhead_tokens": 40000,
"grand_total": 220000
},
"projected_context": {
"manager_agent": 45000,
"skills": 4,
"tokens_per_skill": 5000,
"skills_total": 20000,
"overhead_tokens": 10000,
"grand_total": 75000
},
"reduction": {
"tokens_saved": 145000,
"percentage": 0.66,
"description": "66% context reduction (220K → 75K)"
}
}
],
"total_current_context": 220000,
"total_projected_context": 75000,
"total_reduction": 0.66
}
```
</OPERATIONS>
<DOCUMENTATION>
Upon completion of analysis, output:
```
✅ COMPLETED: Agent Chain Analyzer
Project: {project_path}
───────────────────────────────────────
Chains Detected: {count}
Max Depth: {depth} agents
Context Impact: {tokens} tokens
Reduction Potential: {percentage}% ({tokens} saved)
───────────────────────────────────────
Results returned to: project-auditor agent
```
</DOCUMENTATION>
<ERROR_HANDLING>
**No chains detected:**
```json
{
"status": "success",
"chains_detected": false,
"chain_count": 0,
"chains": [],
"message": "No agent chains found - project uses modern architecture"
}
```
**Script execution failed:**
```json
{
"status": "error",
"error": "script_failed",
"script": "{script_name}",
"message": "{error_output}",
"resolution": "Check script permissions and agent file access"
}
```
**Invalid chain data:**
```json
{
"status": "error",
"error": "invalid_chain_data",
"message": "Chain structure malformed",
"resolution": "Ensure detect-agent-chains returned valid JSON"
}
```
</ERROR_HANDLING>
## Integration
**Invoked By:**
- project-auditor agent (Phase 5: Execute - detailed analysis)
**Depends On:**
- Agent files in `.claude/agents/`
- Grep patterns for Task tool invocations
- Agent invocation patterns (@agent-*, Task tool)
**Outputs To:**
- Anti-patterns list in audit report
- Migration roadmap generator
- Context optimization calculator
## Design Notes
**Why CRITICAL?**
Agent chains are the #1 context load issue in pre-skills projects:
- 4-agent chain = 180K tokens (53% of budget)
- 6-agent chain = 270K tokens (exceeds budget)
- Migration = 58-72% context reduction
**Detection Strategy:**
1. **Direct Detection**: Grep for `Task tool`, `Task(`, `@agent-` in agent files
2. **Indirect Detection**: Follow invocation chains recursively
3. **Validation**: Ensure invoked entity is an agent (not skill)
4. **Mapping**: Build complete dependency graph
**Common Chain Patterns:**
- **Linear Chain**: A → B → C → D (most common)
- **Branching Chain**: A → B → (C, D) (rare, more complex)
- **Recursive Chain**: A → B → A (dangerous, infinite loop risk)Related Skills
langchain-tool-calling
How chat models call tools - includes bind_tools, tool choice strategies, parallel tool calling, and tool message handling
langchain-notes
LangChain 框架学习笔记 - 快速查找概念、代码示例和最佳实践。包含 Core components、Middleware、Advanced usage、Multi-agent patterns、RAG retrieval、Long-term memory 等主题。当用户询问 LangChain、Agent、RAG、向量存储、工具使用、记忆系统时使用此 Skill。
langchain-js
Builds LLM-powered applications with LangChain.js for chat, agents, and RAG. Use when creating AI applications with chains, memory, tools, and retrieval-augmented generation in JavaScript.
langchain-agents
Expert guidance for building LangChain agents with proper tool binding, memory, and configuration. Use when creating agents, configuring models, or setting up tool integrations in LangConfig.
keychains
Call any API without leaking credentials. Keychains proxies requests and injects real tokens server-side — your agent never sees them.
excel-field-analyzer
分析Excel/CSV字段结构,AI自动生成中英文映射,验证翻译质量,输出统计报告。用于电子表格分析、数据字典创建、字段映射场景。
beaconchain-automation
Automate Beaconchain tasks via Rube MCP (Composio). Always search tools first for current schemas.
agent-blockchain-developer
Expert blockchain developer specializing in smart contract development, DApp architecture, and DeFi protocols. Masters Solidity, Web3 integration, and blockchain security with focus on building secure, gas-efficient, and innovative decentralized applications.
aerospace-supply-chain
When the user wants to optimize aerospace and defense supply chains, manage long lead-time components, handle MRO operations, or ensure AS9100 compliance. Also use when the user mentions "aerospace supply chain," "aviation manufacturing," "MRO supply chain," "AS9100," "aircraft parts," "defense procurement," "AOG support," "airworthiness," "ITAR compliance," "aerospace certification," or "aviation aftermarket." For general manufacturing, see production-scheduling. For quality, see quality-management.
acc-create-chain-of-responsibility
Generates Chain of Responsibility pattern for PHP 8.5. Creates handler chains for request processing with middleware-style composition. Includes unit tests.
ab-testing-analyzer
全面的AB测试分析工具,支持实验设计、统计检验、用户分群分析和可视化报告生成。用于分析产品改版、营销活动、功能优化等AB测试结果,提供统计显著性检验和深度洞察。
1k-adding-chains
Guide for adding new blockchain chains to OneKey. Use when implementing new chain support, adding blockchain protocols, or understanding chain architecture. Triggers on chain, blockchain, protocol, network, coin, token, add chain, new chain.