Tutorials · Claude Agent SDK · examples / python / 15_skills_and_prompts.pyExample code

Example code · python

15_skills_and_prompts.py

Skills (dispatch /security-check), CLAUDE.md loading, and system prompt variants.

Raw fileDownload all examples (zip)See its output →

Used in Part 2 §6 · Shaping behaviour: system prompts, CLAUDE.md, output styles

"""15: Skills (dispatch /security-check), CLAUDE.md loading, and system prompt variants."""

import asyncio

from claude_agent_sdk import AssistantMessage, ClaudeAgentOptions, ResultMessage, SystemMessage, ToolUseBlock, query
from demo_util import PROJECT


async def main():
    # --- A: dispatch a project skill by name -------------------------------------------
    print("=== A: /security-check skill ===")
    async for message in query(
        prompt="/security-check",
        options=ClaudeAgentOptions(cwd=PROJECT, setting_sources=["project"], skills="all",
                                   allowed_tools=["Read", "Grep", "Glob"], max_turns=8),
    ):
        if isinstance(message, SystemMessage) and message.subtype == "init":
            print("skills loaded:", message.data.get("skills"))
            print("'security-check' in slash_commands:", "security-check" in message.data.get("slash_commands", []))
        elif isinstance(message, AssistantMessage):
            for block in message.content:
                if isinstance(block, ToolUseBlock):
                    print(f"  tool: {block.name}")
        elif isinstance(message, ResultMessage):
            print("result:\n", message.result)

    # --- B: CLAUDE.md conventions are followed when the project source is loaded --------
    print("\n=== B: CLAUDE.md loaded (setting_sources=['project']) vs not ([]) ===")
    for sources in (["project"], []):
        try:
            async for message in query(
                prompt="Without using any tools: do you have project-specific instructions about type hints? "
                       "Answer yes or no, then quote them if yes.",
                options=ClaudeAgentOptions(cwd=PROJECT, setting_sources=sources, tools=[], max_turns=2),
            ):
                if isinstance(message, ResultMessage):
                    print(f"setting_sources={sources}: {message.result!r}")
        except Exception as error:
            print(f"setting_sources={sources}: ended with error: {error}")

    # --- C: system prompt variants ----------------------------------------------------
    print("\n=== C: system prompt variants ===")
    variants = {
        "default (minimal)": None,
        "claude_code preset + append": {"type": "preset", "preset": "claude_code",
                                        "append": "Always end your reply with the word BANANA."},
        "custom string": "You are a pirate. Answer in one short pirate sentence.",
    }
    for label, sp in variants.items():
        async for message in query(
            prompt="Who are you and what can you do? One sentence.",
            options=ClaudeAgentOptions(cwd=PROJECT, system_prompt=sp, tools=[], max_turns=2,
                                       setting_sources=[]),
        ):
            if isinstance(message, ResultMessage):
                print(f"[{label}] {message.result!r}")


asyncio.run(main())

60 lines · Back to the folder