Use this file to discover all available pages before exploring further.
This cookbook shows how to add Maxim observability and tracing to your Smolagents applications. You’ll learn how to instrument Smolagents, create SQL database tools, and monitor agent interactions with full observability in the Maxim dashboard.
Define a tool that allows the agent to query the database:
@tooldef sql_engine(query: str) -> str: """ Allows you to perform SQL queries on the table. Returns a string representation of the result. The table is named 'receipts'. Its description is as follows: Columns: - receipt_id: INTEGER - customer_name: VARCHAR(16) - price: FLOAT - tip: FLOAT Args: query: The query to perform. This should be correct SQL. """ output = "" with engine.connect() as con: rows = con.execute(text(query)) for row in rows: output += "\n" + str(row) return output
Create an agent with the SQL tool and OpenAI model:
# Initialize the agent with tools and modelagent = CodeAgent( tools=[sql_engine], model=OpenAIServerModel( model_id="gpt-4o-mini", # or "gpt-4o" api_key=os.environ["OPENAI_API_KEY"], # uses .env via load_dotenv() api_base="https://api.openai.com/v1", # optional, default works ),)
Execute queries and observe the agent’s reasoning and tool usage:
# Example queryquery = "Can you give me the name of the client who got the most expensive receipt?"print(f"Query: {query}")print("=" * 50)response = agent.run(query)print(f"Response: {response}")
queries = [ "What is the total amount spent by all customers?", "Which customer left the highest tip?", "What is the average tip percentage?", "Show me all receipts above $20"]for query in queries: print(f"\nQuery: {query}") print("-" * 40) response = agent.run(query) print(f"Response: {response}")
complex_query = """Analyze the receipt data and provide insights about:1. Customer spending patterns2. Tip behavior3. Any interesting trends you notice"""print(f"Complex Analysis Query: {complex_query}")print("=" * 60)response = agent.run(complex_query)print(f"Analysis: {response}")
All agent interactions, tool calls, and SQL queries are automatically traced and can be visualized in your Maxim dashboard. This provides deep insights into:
Agent Reasoning: See how the agent processes queries and decides which tools to use
Tool Usage: Monitor SQL query execution and results
Performance Metrics: Track response times and token usage
Error Handling: Identify and debug any issues with queries or tool calls