Skip to main content

withContext

Provides domain-specific context and background knowledge to the agent. Context gives the agent essential information about the domain it’s operating in, including facts, conventions, operational details, and any other background knowledge needed to respond appropriately. This is critical for specialized agents that need to understand specific business rules, industry practices, or organizational details. Context differs from identity and capabilities:
  • Identity: Who/what the agent is
  • Capabilities: What the agent can do
  • Context: What the agent needs to know
withContext(text: string): this
text
string
required
Contextual information for the agent. Can be multi-line. Should contain factual, relevant information that helps the agent understand its operating environment.
Returns
this
The builder instance for method chaining

Examples

builder.withContext(`
  Our clinic operates Monday-Friday, 9 AM to 5 PM.
  We have three doctors:
  - Dr. Smith specializes in general medicine
  - Dr. Jones specializes in cardiology
  - Dr. Lee specializes in pediatrics
  Average appointment duration is 30 minutes.
  Emergency appointments can be accommodated with 24-hour notice.
`);

withExamples

Provides examples of desired agent behavior through few-shot learning. Examples are one of the most effective techniques for teaching an AI agent how to behave. By showing concrete input-output pairs, you demonstrate the exact pattern of responses you want. This is especially powerful for:
  • Establishing consistent response style
  • Demonstrating proper tool usage
  • Showing how to handle edge cases
  • Teaching domain-specific interaction patterns
Each example should demonstrate a specific aspect of good behavior. The model will learn from these examples and apply similar patterns in its responses.
withExamples(examples: Example[]): this
examples
Example[]
required
An array of Example objects showing input-output pairs. Use either user/assistant (conversational) or input/output (functional). Include explanation to clarify what the example demonstrates.
Returns
this
The builder instance for method chaining

Examples

builder.withExamples([
  {
    user: "What's the weather in Paris?",
    assistant: "I'll check the weather for you. *calls get_weather tool with location: Paris*",
    explanation: "Shows proper tool invocation for weather queries"
  },
  {
    user: "Book me a table for 2 at 7pm",
    assistant: "I'll help you make a reservation. What restaurant would you like?",
    explanation: "Shows how to ask for missing required information"
  }
]);

withErrorHandling

Defines how the agent should handle uncertainty, errors, and ambiguous situations. This method specifies the agent’s behavior when it encounters situations it’s not confident about, including:
  • Ambiguous user requests
  • Missing required information
  • Uncertainty about the correct response
  • Error conditions
  • Requests outside its capabilities
Clear error handling instructions help prevent:
  • Hallucinations (making up information when uncertain)
  • Inappropriate assumptions
  • Unhelpful or incorrect responses
  • Poor user experience during edge cases
withErrorHandling(instructions: string): this
instructions
string
required
Clear guidelines for handling uncertainty and errors. Should specify what the agent should do when it encounters various types of problematic situations. Can be multi-line for complex instructions.
Returns
this
The builder instance for method chaining

Examples

builder.withErrorHandling(
  "When uncertain about a response, acknowledge your uncertainty and ask " +
  "clarifying questions rather than guessing or making assumptions."
);

Build docs developers (and LLMs) love