Agents

Agents provide an agentic loop that automatically handles tool execution, enabling autonomous task completion.

Overview

The Agent class implements a complete agentic workflow:

  1. Send prompt to LLM

  2. LLM may request tool calls

  3. Execute tools automatically

  4. Send results back to LLM

  5. Repeat until task complete or limits reached

Creating an Agent

import { AgenticWork } from '@agentic-work/sdk';

const aw = new AgenticWork({
  apiEndpoint: 'https://api.agenticwork.io',
  authToken: process.env.AGENTICWORK_TOKEN
});

await aw.init();

const agent = aw.createAgent({
  model: 'gpt-4o',
  systemPrompt: 'You are a helpful coding assistant.',
  tools: aw.getMCPTools(),
  maxIterations: 10,
  maxToolCalls: 25,
  onToolCall: (tc) => console.log('Tool called:', tc.name),
  onToolResult: (tr) => console.log('Tool result:', tr.content.slice(0, 100)),
  onText: (text) => process.stdout.write(text)
});

Configuration Options

Option
Type
Default
Description

model

string

Required

LLM model to use

systemPrompt

string

Built-in

Custom system prompt

tools

Tool[]

[]

Tools available to the agent

maxIterations

number

10

Maximum LLM call rounds

maxToolCalls

number

25

Maximum total tool executions

onToolCall

function

-

Callback when tool is invoked

onToolResult

function

-

Callback when tool completes

onText

function

-

Callback for each text chunk

Running an Agent

Streaming Execution

Non-Streaming Execution

Agent Result

The execute() method returns an AgentResult:

Using Platform MCP Tools

Adding Custom Tools

Tool Context

Tools receive a context object with useful utilities:

Use context in tool execution:

Removing Tools

Updating Agent Configuration

Default System Prompt

If no system prompt is provided, agents use:

Callbacks for Observability

Error Handling

Best Practices

  1. Set appropriate limits - Adjust maxIterations and maxToolCalls based on task complexity

  2. Use callbacks - Monitor agent progress with onToolCall and onToolResult

  3. Provide clear prompts - Specific prompts lead to better results

  4. Filter tools - Only provide tools relevant to the task

  5. Handle errors - Implement proper error handling for production use

Last updated