Extend AI capabilities with custom functions and additional information
Tools and context are two powerful ways to enhance Marvin’s capabilities, allowing agents to perform actions beyond their built-in abilities and access additional information when completing tasks.
Tools are Python functions that agents can call during task execution. They allow agents to:
Access external systems and APIs
Perform calculations or data processing
Interact with files, databases, or other resources
Execute custom business logic
Copy
Ask AI
import marvinimport requestsdef search_web(query: str) -> str: """Search the web for information about the given query.""" response = requests.get(f"https://api.search.com?q={query}") return response.json()["results"]# Use the tool in a taskresult = marvin.run( "Research the latest developments in quantum computing", tools=[search_web])
When an agent has access to a tool, it can decide when and how to use it based on the task requirements. The agent sees the tool’s name, docstring, and signature, and can call it with appropriate arguments.
Well-documented: Clear docstrings help the agent understand when and how to use the tool
Type-hinted: Type annotations help the agent understand what inputs and outputs to expect
Copy
Ask AI
from typing import Annotatedfrom pydantic import Field# Define custom types with descriptionsZipCode = Annotated[str, Field(description="A 5-digit US ZIP code")]Temperature = Annotated[float, Field(description="Temperature in Fahrenheit")]def get_weather(location: ZipCode) -> Temperature: """Get the current temperature for the specified ZIP code.""" # Implementation details... return 72.5
# To a specific tasktask_result = marvin.run("Check the weather", tools=[get_weather])# To an agent (available for all tasks the agent performs)weather_agent = marvin.Agent( name="WeatherBot", instructions="You provide weather information", tools=[get_weather, get_forecast])# To a thread (available for all tasks in the thread)with marvin.Thread() as thread: thread.tools = [get_weather, get_forecast] marvin.run("What's the weather like?") marvin.run("Will it rain tomorrow?")
Context is additional information you provide to a task or agent to help it generate better responses. Unlike tools (which are functions the agent can call), context is static data that informs the agent’s understanding.
Copy
Ask AI
import marvin# Provide context to a taskresponse = marvin.run( "Summarize the patient's condition", context={ "medical_records": "Patient presents with fever (101.2°F), cough, and fatigue...", "patient_history": "42-year-old male with history of asthma...", "lab_results": "White blood cell count elevated at 12,000..." })
You can reference context directly in your prompts using string formatting:
Copy
Ask AI
import marvinuser_data = {"name": "Jamie", "goal": "lose weight"}plan = marvin.run( "Create a fitness plan for {name} who wants to {goal}", context=user_data)
Security: Only provide tools that you’re comfortable with the agent using
Error handling: Ensure tools handle errors gracefully and provide helpful error messages
Complexity: Start with simple tools and context, then add complexity as needed
Testing: Test your tools independently before providing them to agents
Observability: Monitor tool usage to understand how agents are using them
By effectively combining tools and context, you can create AI workflows that leverage both the reasoning capabilities of large language models and the precision of traditional code.