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.1.tar.gz (8.6 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.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lean_agents-0.0.1.tar.gz
  • Upload date:
  • Size: 8.6 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.1.tar.gz
Algorithm Hash digest
SHA256 3d2e68fbb1cffdc012e6329b5223fded6f0f1d56b7eb169b7bb8257454a995c6
MD5 55c41d012b99597bb3286f7d40db0dfa
BLAKE2b-256 e0c301624a2d23f1a845f51a473ae9e9c973f9b405e46f8ba4394c5a9af2a9c0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lean_agents-0.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 66b9b505e82814e86704961657c370713da0697fdb03ed64998d19295f65f0a2
MD5 59918b9f74c921c4a85932db58bddcb6
BLAKE2b-256 c47dfea4d25d30496cec094ffcfd66d342fc493f1380c6b9c4759571203190e2

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