Complete guide to error handling in Tawk Agents SDK.
The SDK provides comprehensive error handling with specific error types for different failure scenarios. Understanding these errors helps you build robust agent applications.
- Execution Errors - Max turns, timeouts
- Validation Errors - Guardrails, input validation
- Tool Errors - Tool execution failures
- Handoff Errors - Agent delegation failures
- Approval Errors - Approval workflow failures
Thrown when an agent exceeds the maximum number of turns.
import { run, MaxTurnsExceededError } from 'tawk-agents-sdk';
try {
const result = await run(agent, 'Complex query...', {
maxTurns: 10
});
} catch (error) {
if (error instanceof MaxTurnsExceededError) {
console.error('Agent exceeded max turns');
console.error('Current step:', error.stepNumber);
console.error('Max turns:', error.maxTurns);
// Recovery: Increase maxTurns or simplify query
const retry = await run(agent, 'Simplified query...', {
maxTurns: 20
});
}
}Properties:
stepNumber- Current step when error occurredmaxTurns- Maximum turns allowedmessage- Error message with details
Thrown when a guardrail blocks input or output.
import { run, GuardrailTripwireTriggered } from 'tawk-agents-sdk';
try {
const result = await run(agent, 'User input...');
} catch (error) {
if (error instanceof GuardrailTripwireTriggered) {
console.error('Guardrail blocked:', error.guardrailName);
console.error('Reason:', error.message);
// Recovery: Inform user or retry with different input
return {
error: 'Input blocked by safety guardrail',
reason: error.message
};
}
}Properties:
guardrailName- Name of the guardrail that triggeredtype- 'input' or 'output'message- Reason for blocking
Thrown when a tool execution fails.
import { run, ToolExecutionError } from 'tawk-agents-sdk';
try {
const result = await run(agent, 'Use tool...');
} catch (error) {
if (error instanceof ToolExecutionError) {
console.error('Tool failed:', error.toolName);
console.error('Error:', error.originalError);
// Recovery: Retry tool or use fallback
if (error.toolName === 'databaseQuery') {
// Use cached data or fallback query
return await useFallbackData();
}
}
}Properties:
toolName- Name of the tool that failedargs- Arguments passed to the tooloriginalError- Original error from tool execution
Thrown when an agent handoff fails.
import { run, HandoffError } from 'tawk-agents-sdk';
try {
const result = await run(triageAgent, 'Query...');
} catch (error) {
if (error instanceof HandoffError) {
console.error('Handoff failed:', error.fromAgent, '→', error.toAgent);
console.error('Reason:', error.message);
// Recovery: Fallback to default agent or escalate
return await run(escalationAgent, 'Query...');
}
}Properties:
fromAgent- Agent that attempted handofftoAgent- Target agent for handoffreason- Reason for handoff failure
Thrown when an approval is required but not provided.
import { run, ApprovalRequiredError } from 'tawk-agents-sdk';
try {
const result = await run(agent, 'Delete file...');
} catch (error) {
if (error instanceof ApprovalRequiredError) {
console.error('Approval required for:', error.toolName);
// Recovery: Request approval and retry
const approved = await requestApproval(error.toolName, error.args);
if (approved) {
return await run(agent, 'Delete file...', {
// Pass approval in context
});
}
}
}Properties:
toolName- Tool requiring approvalargs- Tool argumentsmessage- Error message
import {
run,
MaxTurnsExceededError,
GuardrailTripwireTriggered,
ToolExecutionError
} from 'tawk-agents-sdk';
try {
const result = await run(agent, query);
return result;
} catch (error) {
// Handle specific error types
if (error instanceof MaxTurnsExceededError) {
return handleMaxTurnsError(error);
}
if (error instanceof GuardrailTripwireTriggered) {
return handleGuardrailError(error);
}
if (error instanceof ToolExecutionError) {
return handleToolError(error);
}
// Handle unknown errors
console.error('Unknown error:', error);
throw error;
}import { run } from 'tawk-agents-sdk';
async function runWithErrorHandling(agent, query) {
try {
return await run(agent, query);
} catch (error) {
return handleAgentError(error, agent, query);
}
}
function handleAgentError(error, agent, query) {
// Log error
logger.error('Agent error', {
agent: agent.name,
query,
error: error.message,
errorType: error.constructor.name
});
// Return user-friendly error
return {
success: false,
error: 'An error occurred while processing your request',
errorType: error.constructor.name,
canRetry: error instanceof ToolExecutionError
};
}import { run, ToolExecutionError } from 'tawk-agents-sdk';
async function runWithRetry(agent, query, maxRetries = 3) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
return await run(agent, query);
} catch (error) {
if (error instanceof ToolExecutionError && attempt < maxRetries - 1) {
// Retry with exponential backoff
const delay = Math.pow(2, attempt) * 1000;
await new Promise(resolve => setTimeout(resolve, delay));
continue;
}
throw error;
}
}
}import { run } from 'tawk-agents-sdk';
async function runWithFallback(primaryAgent, fallbackAgent, query) {
try {
return await run(primaryAgent, query);
} catch (error) {
console.warn('Primary agent failed, using fallback');
return await run(fallbackAgent, query);
}
}import { run, ToolExecutionError } from 'tawk-agents-sdk';
try {
const result = await run(agent, query);
return result;
} catch (error) {
if (error instanceof ToolExecutionError) {
// Return partial result without tool
return {
success: false,
message: 'Some features are unavailable',
partialResult: 'Basic response without tool results'
};
}
throw error;
}import { run, GuardrailTripwireTriggered } from 'tawk-agents-sdk';
try {
const result = await run(agent, userInput);
return result;
} catch (error) {
if (error instanceof GuardrailTripwireTriggered) {
// Notify user
return {
success: false,
message: 'Your input was blocked by safety filters',
reason: error.message,
suggestion: 'Please rephrase your query'
};
}
throw error;
}// ✅ Good: Handle errors
try {
const result = await run(agent, query);
return result;
} catch (error) {
return handleError(error);
}
// ❌ Bad: Let errors propagate unhandled
const result = await run(agent, query); // Could throw// ✅ Good: Handle specific errors
if (error instanceof MaxTurnsExceededError) {
// Specific handling
}
// ❌ Bad: Generic error handling
catch (error) {
// Too generic
}catch (error) {
logger.error('Agent execution failed', {
agent: agent.name,
query,
error: error.message,
errorType: error.constructor.name,
stack: error.stack
});
throw error;
}catch (error) {
if (error instanceof GuardrailTripwireTriggered) {
return {
error: 'Your input was blocked for safety reasons',
// Don't expose internal details
};
}
}// Retry transient errors
if (error instanceof ToolExecutionError && isTransient(error)) {
return await retryWithBackoff(() => run(agent, query));
}// Execution errors
class MaxTurnsExceededError extends Error {
stepNumber: number;
maxTurns: number;
}
// Validation errors
class GuardrailTripwireTriggered extends Error {
guardrailName: string;
type: 'input' | 'output';
}
// Tool errors
class ToolExecutionError extends Error {
toolName: string;
args: any;
originalError: Error;
}
// Handoff errors
class HandoffError extends Error {
fromAgent: Agent;
toAgent: Agent;
reason: string;
}
// Approval errors
class ApprovalRequiredError extends Error {
toolName: string;
args: any;
}- Core Concepts - Understanding agents
- Features Guide - All SDK features
- API Reference - Complete API documentation
- Flow Diagrams - Guardrail validation flow
Status: ✅ Documentation Complete