Skip to content

feat(hooks): add agent context fields to hook input#36279

Open
abhibarkade wants to merge 2 commits intoanthropics:mainfrom
abhibarkade:feat/hook-agent-context-fields
Open

feat(hooks): add agent context fields to hook input#36279
abhibarkade wants to merge 2 commits intoanthropics:mainfrom
abhibarkade:feat/hook-agent-context-fields

Conversation

@abhibarkade
Copy link
Copy Markdown

@abhibarkade abhibarkade commented Mar 19, 2026

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:

Field Type Description
is_subagent boolean true if the tool call originates from a subagent.
agent_name string Name of the current subagent; empty for the main agent.
parent_session_id string Session ID of the parent context ("" if top-level).
agent_depth integer Nesting level (0 = main agent, 1 = subagent, etc.).

Detailed Changes

📖 Documentation & Skills

  • SKILL.md: Updated the "Hook Input Format" with the new schema and added guidance on using is_subagent for targeted denial messages in the PreToolUse section.
  • 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-sample payloads to include the new fields. Added a --subagent flag 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 run git commands, 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:

# Verify subagent-context sample generation
./test-hook.sh --subagent --create-sample PreToolUse | jq '{is_subagent, agent_name, agent_depth}'
# Output:
# {
#   "is_subagent": true,
#   "agent_name": "git-expert",
#   "agent_depth": 1
# }

…#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
@abhibarkade abhibarkade marked this pull request as ready for review March 19, 2026 16:45
@abhibarkade
Copy link
Copy Markdown
Author

@claude

@abhibarkade
Copy link
Copy Markdown
Author

abhibarkade commented Mar 20, 2026

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 PreToolUse) to know which specific agent is attempting to execute a tool.

🌟 Key Capabilities Enabled:

  • Granular Security Policies: Hook developers can now write logic like "Only allow the git-expert subagent to execute git push" or "Deny all subagents from running npm publish."
  • Targeted Feedback: Return specific, tailored denial messages dependent on whether the caller is a subagent (is_subagent), helping the subagent course-correct without confusing the user.
  • Enhanced Observability: Hooks can now accurately log tool usage by extracting the agent_name, agent_depth, and tracing back through the parent_session_id.

🧪 How to test:

I've updated the test-hook.sh utility to make testing these new fields frictionless. Reviewers can quickly verify the new subagent payload structure without needing to spin up a complex multi-agent flow:

# 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
# }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant