Skip to content

agent.md

Agent API

The Agent class is the core of AgentForge.

Constructor

typescript
new Agent(config: AgentConfig)

AgentConfig

PropertyTypeRequiredDefaultDescription
providerProvider-LLM provider instance
toolsTool[][]Available tools
systemPromptstring-System message
middlewareMiddleware[][]Middleware stack
memoryMemoryConfig-Memory settings
maxIterationsnumber10Max tool loops
temperaturenumber-LLM temperature
maxTokensnumber-Max response tokens

Methods

run()

Execute the agent with a message.

typescript
async run(
  input: string | Message[],
  options?: { signal?: AbortSignal }
): Promise<AgentResponse>

Parameters:

  • input - String message or array of Message objects
  • options.signal - AbortSignal for cancellation

Returns: AgentResponse

typescript
interface AgentResponse {
  id: string;
  content: string;
  messages: Message[];
  toolResults?: ToolResult[];
  usage?: {
    promptTokens: number;
    completionTokens: number;
    totalTokens: number;
  };
}

Example:

typescript
// String input
const response = await agent.run('Hello!');

// Message array
const response = await agent.run([
  { id: '1', role: 'user', content: 'Hello', timestamp: Date.now() },
]);

// With abort signal
const controller = new AbortController();
const response = await agent.run('Hello', { signal: controller.signal });

stream()

Stream the agent response.

typescript
async *stream(
  input: string | Message[],
  options?: { signal?: AbortSignal }
): AsyncIterable<StreamEvent>

Yields: StreamEvent

TypeDataDescription
'content'stringText chunk
'tool_call'ToolCallTool being called
'tool_result'ToolResultTool result
'done'{ content: string }Stream complete

Example:

typescript
for await (const event of agent.stream('Tell me a story')) {
  if (event.type === 'content') {
    process.stdout.write(event.data as string);
  }
}

addTool()

Add a tool at runtime.

typescript
addTool(tool: Tool): void

removeTool()

Remove a tool by name.

typescript
removeTool(name: string): boolean

Returns: true if removed, false if not found

getTools()

Get all registered tools.

typescript
getTools(): Tool[]

setSystemPrompt()

Update the system prompt.

typescript
setSystemPrompt(prompt: string): void

MemoryConfig

typescript
interface MemoryConfig {
  maxMessages?: number;
  maxTokens?: number;
  strategy?: 'sliding-window' | 'summarize' | 'trim-oldest';
}
PropertyTypeDefaultDescription
maxMessagesnumber-Max messages to keep
maxTokensnumber-Max tokens to keep
strategystring'sliding-window'Trimming strategy

Errors

ErrorCodeCause
AgentForgeErrorAGENT_MAX_ITERATIONSExceeded maxIterations
AgentForgeErrorAGENT_ABORTEDAbort signal triggered
ProviderErrorVariousLLM API errors
ToolExecutionErrorTOOL_EXECUTION_FAILEDTool threw error

Released under the MIT License.