Skip to content

Quick Start

This guide will walk you through creating your first Anvil tool.

Start by creating a new project with the Anvil CLI:

Terminal window
mkdir my-agent && cd my-agent
anvil init

This will:

  • Create an anvil_tools/ directory for generated tools
  • Set up .gitignore to protect your API keys
  • Prompt you for your API keys (securely stored in .env)
  • Create an example script

Create a file called main.py:

from dotenv import load_dotenv
from anvil import Anvil
load_dotenv()
# Initialize Anvil
anvil = Anvil()
# Create a tool by describing what you want
weather_tool = anvil.use_tool(
name="get_weather",
intent="Get the current weather for a city using a free weather API"
)
# Run the tool
result = weather_tool.run(city="San Francisco")
print(result)

Run it:

Terminal window
python main.py

The first run will:

  1. Generate Python code for the weather tool
  2. Save it to anvil_tools/get_weather.py
  3. Execute and return the result

Subsequent runs use the cached code instantly.

Check what Anvil generated:

Terminal window
cat anvil_tools/get_weather.py

You’ll see clean, working Python code with:

  • A header showing it’s Anvil-managed
  • The intent hash for change detection
  • A run() function with your logic

Define explicit input parameters for better control:

from anvil import Anvil, InputParam
anvil = Anvil()
stock_tool = anvil.use_tool(
name="get_stock_price",
intent="Get the current stock price for a given ticker symbol",
inputs=[
InputParam(name="symbol", param_type="str", required=True, description="Stock ticker symbol"),
InputParam(name="include_history", param_type="bool", required=False, default=False),
]
)
result = stock_tool.run(symbol="AAPL")
print(result)

Use run_safe() for error handling without exceptions:

result = stock_tool.run_safe(symbol="INVALID")
if result.success:
print(result.data)
else:
print(f"Error: {result.error}")

Convert your tool for any framework:

# For LangChain
from langchain.agents import create_react_agent
lc_tool = stock_tool.to_langchain()
agent = create_react_agent(llm, [lc_tool], prompt)
# For CrewAI
from crewai import Agent
crew_tool = stock_tool.to_crewai()
agent = Agent(role="Analyst", tools=[crew_tool])

Combine tools for complex workflows:

# Create tools
search = anvil.use_tool(name="search", intent="Search the web")
summarize = anvil.use_tool(name="summarize", intent="Summarize text")
# Chain them together
chain = search.pipe(summarize)
result = chain.run(query="Latest AI news")

Useful commands for managing your tools:

Terminal window
# Check system status
anvil doctor
# List all generated tools
anvil list
# Force regeneration of all tools
anvil clean
# Verify tool code is safe
anvil verify get_weather

Use stub mode for testing without LLM calls:

anvil = Anvil(use_stub=True)
# Tools return mock data - no API calls made
tool = anvil.use_tool(name="test", intent="Test tool")
result = tool.run() # Returns stub response