Skip to main content
Intercept and modify agent operations with lifecycle hooks.

Quick Start

1

Simple Usage

import { createHooksManager } from 'praisonai';

const hooks = createHooksManager();

// Log all LLM calls
hooks.register('pre_llm_call', async (context) => {
  console.log('LLM Request:', context.prompt);
  return context;
});

// Validate tool calls
hooks.register('pre_tool_call', async (context) => {
  if (context.toolName === 'dangerous_tool') {
    console.warn('Blocked dangerous tool');
    return null; // Block the operation
  }
  return context;
});

Hook Events

Operation Hooks (Python Parity)

// Code operations
hooks.register('pre_read_code', handler);
hooks.register('post_read_code', handler);
hooks.register('pre_write_code', handler);
hooks.register('post_write_code', handler);

// Command operations
hooks.register('pre_run_command', handler);
hooks.register('post_run_command', handler);

// Prompt operations
hooks.register('pre_user_prompt', handler);
hooks.register('post_user_prompt', handler);

// MCP operations
hooks.register('pre_mcp_tool_use', handler);
hooks.register('post_mcp_tool_use', handler);

LLM/Tool Hooks (CrewAI/Agno Parity)

// LLM call hooks
hooks.register('pre_llm_call', async (ctx) => {
  // Modify prompt, add context, etc.
  return ctx;
});

hooks.register('post_llm_call', async (ctx) => {
  // Log response, filter content, etc.
  return ctx;
});

// Tool call hooks  
hooks.register('pre_tool_call', async (ctx) => {
  // Validate arguments, block dangerous tools
  return ctx;
});

hooks.register('post_tool_call', async (ctx) => {
  // Log results, modify output
  return ctx;
});

Lifecycle Hooks

// Agent lifecycle
hooks.register('agent_start', (ctx) => console.log('Agent started'));
hooks.register('agent_complete', (ctx) => console.log('Agent completed'));

// Run lifecycle
hooks.register('run_started', handler);
hooks.register('run_completed', handler);

Blocking Operations

Return null from a hook to block the operation:
hooks.register('pre_tool_call', async (context) => {
  const dangerousTools = ['delete_file', 'execute_shell'];
  
  if (dangerousTools.includes(context.toolName)) {
    console.warn(`Blocked: ${context.toolName}`);
    return null; // Block the operation
  }
  
  return context; // Allow the operation
});

Modifying Context

Return modified context to change the operation:
hooks.register('pre_llm_call', async (context) => {
  // Add system context
  context.prompt = `[Safety Mode Active]\n${context.prompt}`;
  return context;
});

hooks.register('post_llm_call', async (context) => {
  // Redact sensitive info
  context.response = context.response.replace(/\b\d{16}\b/g, '****');
  return context;
});

Factory Functions

import { createLoggingOperationHooks, createValidationOperationHooks } from 'praisonai';

// Pre-configured logging
const loggingHooks = createLoggingOperationHooks((event, ctx) => {
  console.log(`[${event}]`, ctx);
});

// Pre-configured validation
const validationHooks = createValidationOperationHooks((event, ctx) => {
  if (!ctx.agentId) {
    return { valid: false, reason: 'Missing agentId' };
  }
  return { valid: true };
});

Hook Priority

// Higher priority runs first
hooks.register('pre_llm_call', handler1, { priority: 10 });
hooks.register('pre_llm_call', handler2, { priority: 5 });
// handler1 runs before handler2

Hook Timeout

hooks.register('pre_llm_call', slowHandler, {
  timeout: 5000 // 5 second timeout
});

Callbacks

Display and approval callbacks

Workflow Hooks

Workflow lifecycle hooks

Guardrails

Input/output validation