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

# Events

> React to agent lifecycle events

Agents emit events you can listen to - track progress, log activity, and extend behavior.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    subgraph "Event System"
        A[🤖 Agent] --> B[📡 Events]
        B --> C[📝 Logger]
        B --> D[📊 Analytics]
        B --> E[🔔 Alerts]
    end

    classDef agent fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef tool fill:#189AB4,stroke:#7C90A0,color:#fff
    class A agent
    class B,C,D,E 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 { Agent } from 'praisonai';

    const agent = new Agent({
      instructions: 'You are a helpful assistant'
    });

    agent.on('message', (event) => {
      console.log('Agent said:', event.content);
    });

    agent.on('tool_call', (event) => {
      console.log('Tool used:', event.name);
    });

    await agent.chat('Search for weather');
    ```
  </Step>

  <Step title="With Configuration">
    ```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    agent.on('*', (event) => {
      console.log(`[${event.type}]`, event.data);
    });
    ```
  </Step>
</Steps>

***

## User Interaction Flow

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant Handler as Event Handler
    
    User->>Agent: "Search for weather"
    Agent->>Handler: emit('start')
    Agent->>Handler: emit('tool_call', search)
    Agent->>Handler: emit('message', response)
    Agent->>Handler: emit('complete')
    Agent-->>User: "The weather is..."
```

***

## Configuration Levels

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
// Level 1: Method - Simple listener
agent.on('message', (e) => console.log(e));

// Level 2: Array - Multiple events
agent.on(['start', 'complete'], (e) => {
  console.log(`${e.type} at ${e.timestamp}`);
});

// Level 3: Instance - Event bus
import { AgentEventBus } from 'praisonai';

const bus = new AgentEventBus();

bus.subscribe('agent.*', (event) => {
  sendToAnalytics(event);
});

const agent = new Agent({ eventBus: bus });
```

***

## Event Types

| Event       | When                    |
| ----------- | ----------------------- |
| `start`     | Agent begins processing |
| `message`   | Agent produces output   |
| `tool_call` | Agent calls a tool      |
| `error`     | Something goes wrong    |
| `complete`  | Agent finishes          |

***

## API Reference

<Card title="Events Module" icon="code" href="/docs/sdk/reference/typescript/modules/events">
  Complete events module
</Card>

<Card title="AgentEventBus" icon="robot" href="/docs/sdk/reference/typescript/classes/AgentEventBus">
  Event bus class
</Card>

***

## Best Practices

<AccordionGroup>
  <Accordion title="Log important events">
    Track tool calls and errors for debugging.
  </Accordion>

  <Accordion title="Use wildcards sparingly">
    Subscribe to specific events for better performance.
  </Accordion>

  <Accordion title="Handle errors">
    Always listen for 'error' events in production.
  </Accordion>
</AccordionGroup>

***

## Related

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

  <Card title="PubSub" icon="envelope" href="/docs/js/pubsub">
    Publish-subscribe
  </Card>
</CardGroup>
