Register and share your invite link to earn from video plays and referrals.

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture ๆŠ•็จฟใฏๅ€‹ไบบใฎๆ„่ฆ‹ใงใ™ใ€‚
Joined May 2026
279 Following    408 Followers
# 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#
Show more