Skip to main content
Version: 2.0

Build a Financial Research Agent

This tutorial demonstrates how to create custom Lambda Tools with different return types and use them with AI agents. You'll build a Financial Research Assistant that performs calculations, looks up stock tickers, assesses portfolio risk, and analyzes holdings.

What you will learn

By the end of this tutorial, you'll know how to:

  • ✅ Create Lambda tools with custom Python code.
  • ✅ Work with different return types (float, string, boolean, dictionary).
  • ✅ Use automatic schema discovery from type annotations.
  • ✅ Test tools independently before deployment.
  • ✅ Configure agents with multiple tools.
  • ✅ Update existing tools with new functionality.
  • ✅ Use tools in conversational AI workflows.

What are Lambda Tools?

Lambda tools are user-defined Python 3.12 functions that run in a secure, sandboxed environment:

  • Execute your custom code in an isolated container.
  • Support standard Python modules: json, math, datetime, collections, itertools, functools, re, time, typing.
  • Have resource limits (default: 100MB memory, 30s timeout).
  • Automatically generate input/output schemas from type annotations.

Part 1. Setup and configuration

Before you begin, ensure you have:

  • A Vectara account (sign up here)
  • An API key with tool creation permissions
  • Familiarity with REST APIs

Then, set up your environment and configure API access:

INSTALL DEPENDENCIES

Code example with bash syntax.
1

CONFIGURE API ACCESS

Code example with python syntax.
1

For example:

SETUP COMPLETE

Code example with python syntax.
1

Part 2. Simple Calculator Tool (Returns Float)

Let's start with the simplest Lambda tool, a calculator that returns a numeric value.

Key points

  • Entry point: Must define a process() function.
  • Type annotations: Used for automatic schema discovery.
  • Return type: This tool returns a float, a single numeric value.
  • Code display: We define the function normally first (for syntax highlighting), then convert it to a string.

CREATE CALCULATOR TOOL

Code example with python syntax.
1

For example:

CALCULATOR TOOL CREATED

Code example with python syntax.
1

Test the Calculator

Let's test our calculator with a simple multiplication operation.

TEST CALCULATOR TOOL

Code example with python syntax.
1

Expected Output:

🧪 Test Results:
Type: success
Execution Time: 772ms

🔢 Result: 25 × 4 = {'content': '100.0'}
Return Type: dict

Part 3: Stock Ticker Formatter (Returns String)

Now let's create a tool that returns a string value for a stock ticket symbol formatter.

Define the Function

STOCK TICKER FORMATTER

Code example with python syntax.
1

For example:

TICKER FORMATTER TOOL CREATED

Code example with python syntax.
1

Test the Ticker Formatter

Let's test looking up a company's ticker symbol.

TEST THE TICKER FORMATTER

Code example with python syntax.
1

For example:

TEST RESULTS

Code example with python syntax.
1

Part 4: Risk Checker (Returns Boolean)

This tool returns a bool which is perfect for yes/no decisions and validation.

Key difference

  • Return type: This tool returns bool, True or False.
  • Use case: Decision making, validation, risk assessment.

CREATE RISK CHECKER TOOL

Code example with python syntax.
1

For example:

EXPECTED OUTPUT

Code example with python syntax.
1

Test the Risk Checker

Let's test different scenarios to see the boolean return values.

TEST THE RISK CHECKER

Code example with python syntax.
1

For example:

EXPECTED OUTPUT

Code example with python syntax.
1

Part 5: Portfolio Analyzer (Returns Dictionary)

Finally, let's create a more complex tool that returns structured data as a dict.

Key difference:

  • Return type: This tool returns dict, a structured data with multiple fields,
  • Use case: Complex analysis with multiple output values,

CREATE PORTFOLIO ANALYZER TOOL

Code example with python syntax.
1

For example:

EXPECTED OUTPUT

Code example with python syntax.
1

Test the Portfolio Analyzer

Let's test analyzing a portfolio with multiple holdings.

TEST THE PORTFOLIO ANALYZER

Code example with python syntax.
1

For example:

EXPECTED OUTPUT

Code example with python syntax.
1

Part 6: Update a Tool

Tools can be updated to add new functionality. Let's enhance the calculator with a "power" operation.

UPDATE CALCULATOR WITH POWER OPERATION

Code example with python syntax.
1

For example:

EXPECTED OUTPUT

Code example with python syntax.
1

Part 7: Create an AI agent with all tools

Now let's create an AI agent that can use all four of our tools. The agent intelligently chooses which tool to use based on the user's question.

CREATE FINANCIAL ASSISTANT AGENT

Code example with python syntax.
1

For example:

EXPECTED OUTPUT

Code example with python syntax.
1

Part 8: Create a session and test different tool types

Sessions maintain conversation context. Let's create a session and test each type of tool.

CREATE A SESSION

Code example with python syntax.
1

For example:

EXPECTED OUTPUT

Code example with python syntax.
1

TEST RETURNS

Code example with python syntax.
1

EXPECTED OUTPUT

Code example with python syntax.
1

Part 9: Comprehensive resource cleanup

When you finish experimenting, you can clean up the resources. This section shows multiple ways to delete your tools and agent.

PART 1: LIST ALL RESOURCES

Code example with python syntax.
1

For example:

EXPECTED OUTPUT

Code example with python syntax.
1

INTERACTIVE CLEANUP

Code example with python syntax.
1

For example:

EXPECTED OUTPUT

Code example with python syntax.
1

Manual cleanup alternative

If you prefer to delete resources manually or need to clean up later, here are the curl commands:

MANUAL CLEANUP COMMANDS

Code example with curl syntax.
1

For example:

EXPECTED OUTPUT

Code example with python syntax.
1

Key Takeaways

Lambda Tools with Different Return Types

float - Calculator tool for simple numeric results. str - Ticker formatter for text outputs. bool - Risk checker for yes/no decisions. dict - Portfolio analyzer for structured data.

Code organization

  • Define functions normally (with syntax highlighting!)
  • Use inspect.getsource() to convert to strings
  • Helper function create_tool_from_function() simplifies creation

Tool lifecycle

  1. Create: POST to /v2/tools with your Python code
  2. Test: POST to /v2/tools/{id}/test with sample inputs
  3. Update: PATCH to /v2/tools/{id} with new code
  4. Delete: DELETE to /v2/tools/{id}

Agents

  • Configure multiple tools per agent
  • Agent intelligently chooses the right tool
  • Instructions guide tool usage
  • Sessions maintain conversation context

Resource Management

  • List resources with GET requests
  • Interactive or manual cleanup options
  • Console UI for visual management

Next Steps

Now that you've mastered Lambda Tools, explore:

  • Combine with corpora search: Add document search capabilities
  • Create more tools: Data analysis, API calls, custom calculations
  • Build applications: Integrate into your apps
  • Explore streaming: Real-time responses for better UX
  • Try MCP tools: Connect to Model Context Protocol servers

Additional Resources