<!-- source: https://deepwiki.com/MervinPraison/PraisonAI/14.2-typescript-examples -->

# TypeScript Examples

- README.md
- src/praisonai/README.md

This document provides practical TypeScript/JavaScript code examples demonstrating how to use the PraisonAI framework with the JavaScript SDK. The examples cover installation, basic agent creation, tool integration, multi-agent coordination, task execution patterns, and advanced features.

For Python-based examples with additional features, see [Python Examples](/MervinPraison/PraisonAI/14.1-python-examples). For CLI usage, see [CLI Examples](/MervinPraison/PraisonAI/14.3-cli-examples).

## Installation and Setup

### Installation

Install the PraisonAI JavaScript SDK via npm:

```

```

### Environment Configuration

Set your API key as an environment variable:

```

```

The JavaScript SDK supports the same LLM providers as the Python SDK through LiteLLM integration, including OpenAI, Anthropic, Google Gemini, Groq, Ollama, and 100+ other providers.

**Sources:** [README.md188-192](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L188-L192) [README.md2345-2350](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L2345-L2350)

## Basic Agent Creation

### Single Agent Example

The simplest pattern involves creating an `Agent` instance with `instructions` and calling `start()` with a task prompt:

```

```

The `Agent` class accepts configuration parameters:

**Sources:** [README.md2352-2356](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L2352-L2356)

## Tool Integration

### Tool Function Definition

Tool functions are async functions that agents can invoke to perform specific operations. Tool functions must accept parameters that the agent provides based on task context.

```

```

### Agent with Tools

Agents are configured with tools by passing an array of functions to the `tools` property:

```

```

```

```

**Agent Tool Execution Flow**

**Sources:** [src/praisonai-ts/examples/simple/task-based-agent-tools.ts3-28](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-ts/examples/simple/task-based-agent-tools.ts#L3-L28)

## Multi-Agent Coordination

### Multi-Agent System Structure

The JavaScript SDK supports multi-agent coordination through the `PraisonAIAgents` class (for task-based workflows) or by defining multiple specialized agents that collaborate.

```

```

**Multi-Agent Coordination Architecture**

### Task-Based Multi-Agent Pattern

```

```

**Sources:** [src/praisonai-ts/examples/simple/multi-agent-tools.ts26-44](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-ts/examples/simple/multi-agent-tools.ts#L26-L44)

## Task Execution and Result Handling

### Asynchronous Execution Pattern

The `start()` method returns a Promise that resolves to agent output or an array of results for multi-agent tasks.

```

```

**Multi-Agent Task Execution Sequence**

### Promise-Based Result Handling

```

```

**Sources:** [src/praisonai-ts/examples/simple/task-based-agent-tools.ts41-51](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-ts/examples/simple/task-based-agent-tools.ts#L41-L51) [src/praisonai-ts/examples/simple/multi-agent-tools.ts34-44](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-ts/examples/simple/multi-agent-tools.ts#L34-L44)

## Advanced Features

### Memory Integration

The JavaScript SDK supports memory configuration similar to the Python SDK. Memory enables agents to maintain context across conversations.

```

```

**Memory Types:**

- Short-term : Rolling buffer of recent context
- Long-term : Persistent important facts
- Entity : People, places, organizations
- Episodic : Date-based interaction history

**Sources:** [README.md653-672](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L653-L672)

### Planning Mode

Enable planning mode for agents to create multi-step plans before execution:

```

```

**Planning execution flow:**

1. Agent creates multi-step plan
2. Executes each step sequentially
3. Shows progress with context passing
4. Returns final result

**Sources:** [README.md553-578](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L553-L578)

### Model Configuration

Specify LLM models using LiteLLM format:

```

```

**Supported Providers:**

- OpenAI (gpt-4o, gpt-4o-mini, gpt-3.5-turbo)
- Anthropic (claude-3-opus, claude-3-sonnet, claude-3-haiku)
- Google Gemini (gemini-pro, gemini-1.5-pro)
- Groq (llama3-70b, mixtral-8x7b)
- Ollama (local models)
- 100+ providers via LiteLLM

**Sources:** [README.md482-515](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L482-L515)

## Integration Examples

### MCP (Model Context Protocol)

Use MCP servers to provide external tools to agents:

```

```

**MCP Features:**

- stdio, HTTP, WebSocket, SSE transports
- Session management with Mcp-Session-Id
- Protocol versioning
- Automatic tool discovery

**Sources:** [README.md1412-1449](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L1412-L1449)

### A2A (Agent-to-Agent Protocol)

Expose agents as A2A servers for inter-agent communication:

```

```

**A2A Features:**

- Agent discovery via agent.json
- Auto-generated skills from tools
- Stateful task lifecycle
- SSE streaming for real-time updates

**Sources:** [README.md1483-1523](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/README.md?plain=1#L1483-L1523)

## Code Entity Mapping

The TypeScript examples utilize specific classes and methods from the PraisonAI SDK:

```

```

**TypeScript SDK Class Structure**

**Sources:** [src/praisonai-ts/examples/simple/task-based-agent-tools.ts1-51](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-ts/examples/simple/task-based-agent-tools.ts#L1-L51) [src/praisonai-ts/examples/simple/multi-agent-tools.ts1-44](https://github.com/MervinPraison/PraisonAI/blob/ab80807b/src/praisonai-ts/examples/simple/multi-agent-tools.ts#L1-L44)
