> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Callbacks

> Global display and approval callbacks for agents

Register global callbacks for display events and approval requests.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    App([App]) --> CB[Callbacks]
    CB --> Agent[Agent]

    classDef agent fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef tool fill:#189AB4,stroke:#7C90A0,color:#fff

    class Agent agent
    class App,CB tool
    classDef agent fill:#8B0000,color:#fff
    classDef tool fill:#189AB4,color:#fff
```

## Quick Start

<Steps>
  <Step title="Simple Usage">
    ```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    import { registerDisplayCallback, DisplayTypes } from praisonai;

    registerDisplayCallback(DisplayTypes.AGENT_OUTPUT, (data) => {
      console.log(`[${data.agentName}] ${data.content}`);
    });
    ```
  </Step>

  <Step title="With Approval">
    ```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    import { registerApprovalCallback } from praisonai;

    registerApprovalCallback(async (request) => ({
      approved: request.risk === low,
    }));
    ```
  </Step>
</Steps>

***

## Display Callbacks

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { registerDisplayCallback, DisplayTypes } from 'praisonai';

// Register callback for agent output
registerDisplayCallback(DisplayTypes.AGENT_OUTPUT, (data) => {
  console.log(`[${data.agentName}] ${data.content}`);
});

// Register callback for tool calls
registerDisplayCallback(DisplayTypes.TOOL_CALL, (data) => {
  console.log(`Tool: ${data.toolName}`);
});

// Async callback
registerDisplayCallback(DisplayTypes.LLM_RESPONSE, async (data) => {
  await saveToDatabase(data);
}, true); // isAsync = true
```

## Display Types

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { DisplayTypes } from 'praisonai';

// Agent events
DisplayTypes.AGENT_OUTPUT     // Agent response
DisplayTypes.AGENT_THINKING   // Agent reasoning

// Tool events
DisplayTypes.TOOL_CALL        // Before tool execution
DisplayTypes.TOOL_RESULT      // After tool execution

// LLM events
DisplayTypes.LLM_REQUEST      // Before LLM call
DisplayTypes.LLM_RESPONSE     // After LLM call

// Status events
DisplayTypes.ERROR            // Error occurred
DisplayTypes.WARNING          // Warning
DisplayTypes.INFO             // Information
DisplayTypes.DEBUG            // Debug info

// Workflow events
DisplayTypes.WORKFLOW_START   // Workflow started
DisplayTypes.WORKFLOW_COMPLETE // Workflow done
DisplayTypes.STEP_START       // Step started
DisplayTypes.STEP_COMPLETE    // Step done
```

## Approval Callbacks

Request human approval for risky operations:

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { registerApprovalCallback, requestApproval } from 'praisonai';

// Register approval handler
registerApprovalCallback(async (request) => {
  console.log(`Approval needed: ${request.action}`);
  console.log(`Risk level: ${request.risk}`);
  console.log(`Arguments:`, request.arguments);
  
  // Auto-approve low risk
  if (request.risk === 'low') {
    return { approved: true };
  }
  
  // For higher risk, prompt user
  const answer = await promptUser(`Approve ${request.action}?`);
  return { 
    approved: answer === 'yes',
    reason: answer === 'yes' ? undefined : 'User denied'
  };
});

// Request approval somewhere in your code
const decision = await requestApproval({
  action: 'delete_file',
  arguments: { path: '/important/file.txt' },
  risk: 'high',
  toolName: 'file_operations',
  description: 'Delete an important file'
});

if (decision.approved) {
  // Proceed with operation
} else {
  console.log('Denied:', decision.reason);
}
```

## Risk Levels

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
type RiskLevel = 'low' | 'medium' | 'high' | 'critical';

// low: Safe operations, usually auto-approved
// medium: Some caution needed
// high: Requires explicit approval
// critical: Always requires human confirmation
```

## Callback Management

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import {
  registerDisplayCallback,
  unregisterDisplayCallback,
  getRegisteredDisplayTypes,
  clearAllCallbacks
} from 'praisonai';

// Register
const handler = (data) => console.log(data);
registerDisplayCallback('agent_output', handler);

// Check registered types
const types = getRegisteredDisplayTypes();
console.log('Registered:', types);

// Unregister specific callback
unregisterDisplayCallback('agent_output', handler);

// Clear all callbacks
clearAllCallbacks();
```

## Execute Callbacks

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { executeCallback, executeSyncCallback } from 'praisonai';

// Execute async (preferred)
await executeCallback('agent_output', {
  content: 'Hello!',
  agentName: 'Assistant'
});

// Execute sync only
executeSyncCallback('agent_output', {
  content: 'Hello!',
  agentName: 'Assistant'
});
```

## Related

<CardGroup cols={2}>
  <Card title="Hooks Manager" icon="book" href="/docs/js/hooks-manager">
    Operation hooks
  </Card>

  <Card title="Workflow Hooks" icon="book" href="/docs/js/workflow-hooks">
    Workflow lifecycle
  </Card>

  <Card title="Guardrails" icon="book" href="/docs/js/guardrails">
    Input/output validation
  </Card>
</CardGroup>
