Skip to main content

Overview

This example demonstrates using Monty to analyze team expense data across multiple users. Instead of making 50+ sequential tool calls, the LLM writes a loop that processes each team member’s expenses in code.
This example is adapted from Anthropic’s Programmatic Tool Calling cookbook.

Why This Example Matters

With traditional tool calling, analyzing expenses for a team would require:
  1. Call get_team_members() → returns 5 members
  2. For each member:
    • Call get_expenses(user_id, quarter, category) → returns 10-15 expense items
    • Call get_custom_budget(user_id) → returns budget or null
  3. Process results in the LLM
This floods the context window with hundreds of expense items. With Monty:
  • The loop runs in the sandbox
  • Only the final summary returns to the host
  • Token usage drops dramatically

The Task

Analyze Q3 travel expenses for the Engineering team and identify who exceeded their budget (standard $5,000 or custom).

Code Structure

Type Definitions

The Sandbox Code

Execution

Example Data

Team Members

Expense Data

Each user has 8-15 expense line items with details like:

Custom Budgets

Expected Output

Bob Smith spent over the standard budget but has a custom budget of $7,000, so he’s not flagged as over budget.

Key Benefits

1

Loops in Code

The for member in team_members loop is natural in Python, but would require complex orchestration with tool calls.
2

Conditional Logic

The code checks if expenses exceed the standard budget, then conditionally fetches custom budgets. This would require multiple LLM round-trips with tool calling.
3

In-Sandbox Computation

Summing expense amounts happens in the sandbox. The LLM doesn’t need to do mental math or see every expense item.
4

Reduced Token Usage

Only the final summary leaves the sandbox. With tool calling, every expense item (50+ items) would flood the context.

Running the Example

Async Patterns

All external functions are async, and Monty handles them seamlessly:
For parallel execution, use asyncio.gather() to fire multiple calls at once instead of awaiting each one sequentially.

Next Steps

  • Explore the full source in examples/expense_analysis/
  • Try Web Scraper for browser automation
  • See SQL Playground for file mounting and SQL queries