登録して招待リンクを共有すると、動画再生報酬と紹介報酬を獲得できます。

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture 投稿は個人の意見です。
参加 May 2026
279 フォロー中    408 ファン
# Useful but Little-Known Features of Claude Agent SDK 🌍 Need different agent configurations based on runtime conditions? Factory functions let you dynamically generate agent definitions! Claude Agent SDK supports the factory function pattern to create customized AgentDefinitions on the fly, adapting prompts, models, and tools to runtime context. 📌 Title: Dynamic Agent Definitions (Factory Functions) 🔗 URL: 🧩 Overview By creating factory functions that return `AgentDefinition`, you can dynamically generate agents customized to runtime conditions such as security level, user permissions, or environment. Agents are created at query time, so each request can use different configurations. For example, a strict security review can use the `opus` model while a routine review uses `sonnet`, all from the same factory function. 🛠 How to Use ```python # Python - generate agents based on security level from claude_agent_sdk import query, ClaudeAgentOptions, AgentDefinition def create_security_agent(security_level: str) -> AgentDefinition: is_strict = security_level == "strict" return AgentDefinition( description="Security code reviewer", prompt=f"You are a {'strict' if is_strict else 'balanced'} security reviewer...", tools=["Read", "Grep", "Glob"], model="opus" if is_strict else "sonnet", # Switch model by importance ) # Call factory at query time async for message in query( prompt="Review this PR for security issues", options=ClaudeAgentOptions( allowed_tools=["Read", "Grep", "Glob", "Agent"], agents={ "security-reviewer": create_security_agent("strict") }, ), ): if hasattr(message, "result"): print(message.result) ``` ```typescript // TypeScript function createSecurityAgent(level: "basic" | "strict"): AgentDefinition { const isStrict = level === "strict"; return { description: "Security code reviewer", prompt: `You are a ${isStrict ? "strict" : "balanced"} security reviewer...`, tools: ["Read", "Grep", "Glob"], model: isStrict ? "opus" : "sonnet", }; } for await (const message of query({ prompt: "Review this PR for security issues", options: { allowedTools: ["Read", "Grep", "Glob", "Agent"], agents: { "security-reviewer": createSecurityAgent("strict") } } })) { if ("result" in message) console.log(message.result); } ``` 🏗 Integration into Production Systems - Assign different models and tool sets based on user permission levels or subscription plans - Read conditions from environment variables or config files to generate environment-appropriate agents - Switch `model` based on task importance to optimize the cost-quality tradeoff - Combine multiple factory functions to dynamically compose diverse agent teams 💡 Use Cases 🔐 Use opus for critical security reviews, sonnet for routine reviews 👥 Multi-tenant systems that dynamically adjust available tools based on user permissions 🌐 Generate agents with localized prompts based on region or language settings ⚠️ Caveats - Factory functions are called synchronously at query time, so avoid heavy processing inside them - Generated agents follow the same constraints as regular AgentDefinitions (e.g., subagents cannot spawn subagents) - Since configurations are generated dynamically, log which configuration was used for easier debugging ✨ The factory function pattern lets one codebase serve diverse use cases with tailored agents. Instead of hardcoding conditionals, delegate to factories! #ClaudeAgentSDK# #AIAgent#
もっと見る