No mechanism to "reject and explain" in the same way that Claude Code allows
Claude code has this native "3. No, and tell Claude what to do differently (esc)". This sends a system message saying that the user is rejecting and then immediately after, sends a user message with the user's rejection reason. I do not think there is a mechanism to do this within the SDK? There is PermissionResultDeny, but this only allows a system message, there is no option for a user message explaining the rejection after. I have tried just prepending "User rejection reason:" to the user message but the LLM thinks this rejection is coming from the system so often fails to interpret it correctly (instead getting confused why the system is sending this error message). There are also no docs covering this pattern (there is only one example here which just has a generic user rejection message without explanation https://github.com/anthropics/claude-agent-sdk-python/blob/main/examples/tool_permission_callback.py).
Would greatly appreciate a solution to this which matches the CLI logic more closely or a workaround
https://github.com/EdanStarfire/claudecode_webui/blob/4f964992413b6957384b85b52bbad76a025d17c4/src/web_server.py#L1896-L1903
I have an working example at the link above. This is fully supported:
if clarification_message:
# Deny with clarification - let SDK continue with user guidance
response = {
"behavior": "deny",
"message": clarification_message,
"interrupt": False # CRITICAL: Allow SDK to continue
}
It's just a normal PermissionResultDeny response with the message providing the "pivot guidance". Ensure you set interrupt = False or else it'll stop immediately at the denied tool use.