Cosma Core: Agent Class for Composable Agentic Workflows

This notebook defines the Agent class, which provides the foundation for composable agentic workflows using Cosma. It includes: - A flexible Agent class for creating tool-using LLM agents. - Support for composable and extensible AI workflows. - Integration with cosette, OpenAI API, and Anthropic-recommended patterns.

Author

Charles F. Vardeman II

Note

This nbdev notebook was dialog engineered in the “Extending Cosette with more agentic functionality”. There is an example walkthrough of using the Agent class in the “Example Agent” notebook.

Basic Tools

Example tools for demonstrating agent functionality

Tools must be designed with clear documentation and examples for the LLM to use them effectively. Following Anthropic’s guidance: 1. Use clear, descriptive parameter names 2. Include comprehensive docstrings with examples 3. Specify input formats and constraints 4. Show example usage patterns


source

solve_math

 solve_math (expression:str)

*Evaluates mathematical expressions using a safe subset of Python’s math operations.

The tool supports these operations: - Basic arithmetic: +, -, *, / - Functions: sqrt, pow, sin, cos - Constants: pi

Examples: >>> solve_math(“2+2”) 4.0 >>> solve_math(“sqrt(16)”) 4.0 >>> solve_math(“sin(pi/2)”) 1.0

Input Format: - Use standard mathematical notation - Write functions in lowercase: sqrt(), sin(), cos() - Use parentheses for function arguments: sqrt(16)

Safety: - Only whitelisted math operations are allowed - No arbitrary Python code execution*

Type Details
expression str Mathematical expression as a string (e.g. “2+2”, “sqrt(16)”)
Returns float Numerical result of the evaluation

Agent Class

Core class for building LLM-powered agents with tools and memory

The Agent class provides a high-level interface for creating LLM agents that can: - Maintain conversation history - Use well-documented tools effectively - Follow specific roles and system prompts - Manage context window automatically


source

Agent

 Agent (role:str, model:str, tools:List[Callable]=<factory>,
        system:Optional[str]=None, memory_size:int=10)

*An Agent that can perform tasks using an LLM and optional tools.

The Agent maintains its own conversation state and can use tools to perform actions. It follows Anthropic’s best practices for tool usage and prompting.

Args: role: Description of agent’s role (e.g. “math tutor”) model: LLM model to use (from cosette.models) tools: Optional list of callable tools with type hints and docstrings system: Override default system prompt memory_size: Number of conversation turns to retain

Example: ```python # Create a math tutor agent math_agent = Agent( role=“math tutor”, model=“gpt-4o”, tools=solve_math, system=“You are a helpful math tutor. Show your work and verify with tools.” )

# Use the agent
response = math_agent.run_with_tools("What is sqrt(16) + 7?")
```*

source

Agent.run_with_tools

 Agent.run_with_tools (prompt:str, max_steps:int=5, **kwargs)

*Execute a conversation turn with automatic tool usage.

Uses cosette’s toolloop to allow the model to: 1. Analyze the prompt 2. Choose appropriate tools 3. Call tools with proper parameters 4. Use results to form response

Args: prompt: User’s input message max_steps: Maximum number of tool calls (default: 5) **kwargs: Additional arguments passed to toolloop

Returns: The model’s final response after tool usage

Example: python agent = Agent(role="math tutor", model="gpt-4o", tools=[solve_math]) response = agent.run_with_tools("What is sqrt(16) + sin(pi/2)?")*


source

Agent.show

 Agent.show ()

*Display agent configuration and conversation history.

Shows: - Current role and model - System prompt - Available tools - Token usage statistics - Full conversation history*