feat(hooks): add agent context fields to hook input#36279
Open
abhibarkade wants to merge 2 commits intoanthropics:mainfrom
Open
feat(hooks): add agent context fields to hook input#36279abhibarkade wants to merge 2 commits intoanthropics:mainfrom
abhibarkade wants to merge 2 commits intoanthropics:mainfrom
Conversation
…#36270, anthropics#6885) Add four agent-context fields to the hook input JSON schema so hook handlers can distinguish main-agent tool calls from subagent tool calls. New fields available in all hook events: - is_subagent (bool) – true when called from a subagent - agent_name (string) – subagent name; empty for main agent - parent_session_id (string)– parent session ID; empty at top level - agent_depth (int) – nesting depth (0 = main agent) Changes: - docs: update SKILL.md Hook Input Format section with new fields and add agent-aware guidance to the PreToolUse section - docs: add 'Agent-Aware Hook Patterns' section to advanced.md with bash and Python examples (targeted denial messages, security policy, audit logging) - docs: add Pattern 11 Agent-Context Branching to patterns.md - test: update test-hook.sh --create-sample payloads to include all four agent-context fields; add --subagent flag for subagent samples - examples: add agent-aware-bash-validator.sh – bash hook showing how to emit targeted MCP redirect instructions per agent context - examples: add validate-git-agent.py – Python hook enforcing that only the @git-expert subagent may run git commands, with a transcript-parsing fallback for older Claude Code builds - examples: update bash_command_validator_example.py to extract and branch on is_subagent, agent_name, and agent_depth Closes: anthropics#36270 Related: anthropics#6885
4 tasks
Author
This was referenced Mar 20, 2026
Author
|
Hi team 👋, I've just marked this PR as ready for review. To provide a bit more context, this PR directly addresses the need for agent-aware security policies and observability (resolving #6885). With the increasing use of multi-agent workflows, it's critical for hook handlers (like 🌟 Key Capabilities Enabled:
🧪 How to test:I've updated the # 1. Generate a sample payload simulating a subagent
./test-hook.sh --subagent --create-sample PreToolUse | jq '{is_subagent, agent_name, agent_depth}'
# Expected Output:
# {
# "is_subagent": true,
# "agent_name": "git-expert",
# "agent_depth": 1
# } |
This was referenced Mar 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds documentation, test utilities, and examples for four new agent-context fields in the Claude Code hook input JSON. These fields allow hook handlers to distinguish between the main agent and subagents, enabling more precise security policies, targeted messaging, and better observability in multi-agent workflows.
Fixes issue #6885
New Fields added to Hook Input:
is_subagenttrueif the tool call originates from a subagent.agent_nameparent_session_id""if top-level).agent_depth0= main agent,1= subagent, etc.).Detailed Changes
📖 Documentation & Skills
SKILL.md: Updated the "Hook Input Format" with the new schema and added guidance on usingis_subagentfor targeted denial messages in thePreToolUsesection.advanced.md: Added a new "Agent-Aware Hook Patterns" section with concrete Bash and Python examples for security logic and audit logging.patterns.md: Added Pattern 11: Agent-Context Branching to demonstrate tiered policies.🧪 Test Utilities
test-hook.sh: Updated all--create-samplepayloads to include the new fields. Added a--subagentflag to generate subagent-context test data easily.🚀 Example Hooks
agent-aware-bash-validator.sh: Demonstrates tailored denial messages based on agent context.validate-git-agent.py: Enforces a policy allowing only specific subagents to rungitcommands, with a transcript-parsing fallback.bash_command_validator_example.py: Updated existing example to show context field extraction and prefixing.Verification Results
Sample Payload Verification: