SDK HITL Approval Example
This example demonstrates how to use the PromptKit SDK to handle Human-in-the-Loop (HITL) tool approval workflows in your application.
Overview
The SDK provides high-level APIs for detecting and managing pending tool approvals:
conversation.HasPendingTools() - Check if conversation has pending approvals
conversation.GetPendingTools() - Get detailed information about pending tools
conversation.AddToolResult() - Provide approved tool results
conversation.Continue() - Resume execution after approval
Use Case
This example shows a customer support bot that requires approval before performing high-risk actions like:
- Issuing refunds
- Closing accounts
- Escalating to management
Running the Example
cd sdk/examples/hitl-approval
go run .
Expected Flow
- User requests a refund
- Bot recognizes this requires approval
- Tool execution returns pending status
- Application displays approval UI to supervisor
- Supervisor approves the action
- Application adds approved tool result
- Conversation continues with final response
Code Structure
main.go - Complete HITL workflow demonstration
mock_provider.go - Simulated LLM provider
async_refund_tool.go - Example async tool with approval logic
Key Concepts
response, err := conversation.Send(ctx, "I want a refund for order #12345")
if err != nil {
// Handle error
}
// Check if approval is needed
if len(response.PendingTools) > 0 {
// Show approval UI
for _, pending := range response.PendingTools {
fmt.Printf("Approval needed: %s\n", pending.Message)
fmt.Printf("Risk level: %s\n", pending.Metadata["risk_level"])
}
}
Adding Approved Results
// After supervisor approval
approvedResult := `{"status":"approved","amount":149.99,"transaction_id":"txn_123"}`
err = conversation.AddToolResult(toolCallID, approvedResult)
if err != nil {
// Handle error
}
Resuming Execution
// Continue conversation with approved result
finalResponse, err := conversation.Continue(ctx)
if err != nil {
// Handle error
}
fmt.Printf("Final response: %s\n", finalResponse.Content)
Integration with Real Systems
In a production application, you would:
- Persist conversation state between approval and resumption
- Send notifications to approvers (email, Slack, webhook)
- Track approval metadata (who approved, when, reason)
- Implement timeout handling for expired approvals
- Audit trail for compliance
See Also