Skip to main content

A lightweight Python library for building AI agents with function calling capabilities

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lean_agents-0.0.2.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lean_agents-0.0.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file lean_agents-0.0.2.tar.gz.

File metadata

  • Download URL: lean_agents-0.0.2.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for lean_agents-0.0.2.tar.gz
Algorithm Hash digest
SHA256 79d35a7dcad3ecf7c26c06496bf61f51fc3797c87712bfdf74d3470c397e9dd2
MD5 ef24f8e87c5167820f167ee1fa2ecb0c
BLAKE2b-256 38ee6ffa8eb0cae494f043b06fbd0bf81b5b62469bee1fdef31621e128598b4b

See more details on using hashes here.

File details

Details for the file lean_agents-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: lean_agents-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for lean_agents-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 687d0073ac0d655cffec13d5eb82a5195c7ca0c7ad1fa753e3edf4b998a9807e
MD5 3a9e04372e2ae2c5d68b1123ed515365
BLAKE2b-256 a4648033a80d016230b6bd32eb4dc160f1749bbe590dc638f8268b1dadbb4cdd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page