Skip to main content

Lean Agents

A lightweight Python library for building AI agents with function calling capabilities using OpenAI's API.

Features

  • Separation of Concerns - Agent configuration separate from execution logic
  • Async/Await Support - Built with asyncio for efficient concurrent operations
  • Automatic Schema Generation - Convert Python functions to OpenAI function calling schemas
  • Multiple Tools - Support for multiple tools/functions per agent
  • Type-Safe - Full type hints support for better IDE integration
  • Streaming Responses - Real-time response processing

Installation

# Using uv (recommended)
uv add lean-agents

# Using pip
pip install lean-agents

Quick Start

import asyncio
from lean_agents import Agent, Runner

def add(a: int, b: int) -> int:
    """Add two numbers together and return the result."""
    return int(a) + int(b)

async def main():
    # Create an agent configuration
    agent = Agent(
        instructions="You are a helpful assistant that can handle adding numbers with tool `add`.",
        tools=[add],
        model="gpt-4o-mini"
    )

    # Run the agent (async)
    result = await Runner.run(agent, "What is 123 + 456?")
    print(result.final_output)

asyncio.run(main())

Usage

Defining Tools

Tools are simple Python functions with type hints and docstrings:

def multiply(a: int, b: int) -> int:
    """Multiply two numbers together."""
    return int(a) * int(b)

def get_weather(city: str) -> str:
    """Get the weather for a city."""
    # Your implementation here
    return f"Weather in {city}: Sunny, 25°C"

Creating an Agent

agent = Agent(
    instructions="Your system instructions here",
    tools=[multiply, get_weather],
    model="gpt-4o-mini",  # Optional: defaults to gpt-4o-mini
    api_key="your-api-key"  # Optional: uses OPENAI_API_KEY env var if not provided
)

Running the Agent

Lean Agents provides both async and sync execution methods, similar to OpenAI's agents SDK:

Async execution (recommended):

async def run_example():
    # Execute agent asynchronously
    result = await Runner.run(agent, "What is 15 times 23?")

    # Access the final answer
    print(result.final_output)

    # Access all interaction items (messages, function calls, etc.)
    for item in result.items:
        print(item)

Sync execution:

def run_example_sync():
    # Execute agent synchronously
    result = Runner.run_sync(agent, "What is 15 times 23?")

    # Access the final answer
    print(result.final_output)

Examples

See the examples/ directory for more examples:

  • basic_example.py - Simple addition example
  • advanced_example.py - Multiple tools and complex interactions

To run an example:

cd examples
python basic_example.py

API Reference

Agent

Agent configuration object that holds instructions, tools, and model settings.

Agent(
    instructions: str,
    tools: list[Callable],
    model: str = "gpt-4o-mini",
    api_key: str | None = None
)

Parameters:

  • instructions: System instructions that guide the agent's behavior
  • tools: List of callable functions that the agent can use
  • model: OpenAI model name (default: "gpt-4o-mini")
  • api_key: OpenAI API key (if not provided, uses OPENAI_API_KEY env var)

Runner

Static executor class for running agents. Provides both async and sync methods.

Static Methods:

async run(agent: Agent, input: str) -> RunResult

Run the agent asynchronously with user input.

Parameters:

  • agent: The Agent configuration to execute
  • input: The user's input text

Returns: RunResult object with:

  • final_output: The final text response from the agent
  • items: List of all response items (messages, function calls, reasoning, etc.)

Example:

result = await Runner.run(agent, "Your question here")
print(result.final_output)

run_sync(agent: Agent, input: str) -> RunResult

Run the agent synchronously with user input.

Parameters:

  • agent: The Agent configuration to execute
  • input: The user's input text

Returns: RunResult object (same as async version)

Example:

result = Runner.run_sync(agent, "Your question here")
print(result.final_output)

RunResult

Result object returned by Runner methods.

Attributes:

  • final_output (str): The final text response from the agent
  • items (list): List of all response items

Requirements

  • Python >= 3.10
  • openai >= 1.0.0

Development

# Clone the repository
git clone https://github.com/yourusername/lean-agents.git
cd lean-agents

# Install with development dependencies
uv sync --extra dev

# Run tests
pytest

# Format code
black src/

# Lint code
ruff check src/

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Metadata

Release files for lean-agents 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lean-agents 0.0.2
File Size Uploaded
lean_agents-0.0.2.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lean-agents 0.0.2
File Interpreter ABI Platform
lean_agents-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 15.9 kB

Release files / lean_agents-0.0.2.tar.gz

Download URL lean_agents-0.0.2.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
79d35a7dcad3ecf7c26c06496bf61f51fc3797c87712bfdf74d3470c397e9dd2
BLAKE2b-256 checksum
How to use checksums
38ee6ffa8eb0cae494f043b06fbd0bf81b5b62469bee1fdef31621e128598b4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / lean_agents-0.0.2-py3-none-any.whl

Download URL lean_agents-0.0.2-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
687d0073ac0d655cffec13d5eb82a5195c7ca0c7ad1fa753e3edf4b998a9807e
BLAKE2b-256 checksum
How to use checksums
a4648033a80d016230b6bd32eb4dc160f1749bbe590dc638f8268b1dadbb4cdd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page