Skip to main content

LLM Tool Harness

PyPI version License: MIT Python Versions

A Python library to easily build LLM agents with tool-using capabilities, allowing them to interact with external systems and perform actions.

Key Features

  • Simple Agent Creation: Quickly set up an LLM-powered agent.
  • Easy Tool Definition: Define custom tools with clear schemas and connect them to your Python functions.
  • Pluggable LLM Providers: Start with Anthropic (Claude) and easily extend to other LLM providers.
  • Conversation Management: Automatically handles conversation history and the flow of tool calls and results.
  • Extensible: Designed to be a foundation for more complex agentic systems.

Installation

pip install llm-tool-harness

You will also need to set up API keys for the LLM providers you intend to use. For Anthropic:

export ANTHROPIC_API_KEY="your_anthropic_api_key_here"

Quick Start

Here's how to create a simple agent that can use a bash tool:

import os
import subprocess
from llm_tool_harness import LLMAgent, Tool, AnthropicProvider, ToolInputSchema

# 1. Define your tool implementation function
def execute_bash_command(command: str) -> str:
    """Execute a bash command and return a formatted string with the results."""
    print(f"Executing bash command: {command}")
    try:
        result = subprocess.run(
            ["bash", "-c", command],
            capture_output=True,
            text=True,
            timeout=30
        )
        output = f"STDOUT:\n{result.stdout}\nSTDERR:\n{result.stderr}\nEXIT CODE: {result.returncode}"
        print(f"Bash output:\n{output}")
        return output
    except subprocess.TimeoutExpired:
        return f"Error: Command '{command}' timed out after 30 seconds."
    except Exception as e:
        return f"Error executing command '{command}': {str(e)}"

def main():
    # Ensure ANTHROPIC_API_KEY is set (or handle error)
    if "ANTHROPIC_API_KEY" not in os.environ:
        print("Error: ANTHROPIC_API_KEY environment variable not set.")
        return

    # 2. Create an LLM provider instance
    # Replace with your desired Claude model, e.g., "claude-3-opus-20240229", "claude-3-haiku-20240307"
    anthropic_llm = AnthropicProvider(model="claude-3-sonnet-20240229", max_tokens=2000)

    # 3. Create the LLM Agent
    agent = LLMAgent(
        llm_provider=anthropic_llm,
        system_prompt="""You are a helpful AI assistant with access to a bash terminal.
        You can help the user by executing commands and interpreting the results.
        Be careful with destructive commands and always ask for confirmation if unsure.
        You have access to the 'bash' tool which allows you to run shell commands."""
    )

    # 4. Define the tool schema and create a Tool object
    bash_input_schema: ToolInputSchema = {
        "type": "object",
        "properties": {
            "command": {
                "type": "string",
                "description": "The bash command to execute (e.g., 'ls -l', 'echo hello')"
            }
        },
        "required": ["command"]
    }
    bash_tool = Tool(
        name="bash",
        description="Execute bash shell commands on the local system and return the output.",
        input_schema=bash_input_schema,
        implementation=execute_bash_command
    )

    # 5. Register the tool with the agent
    agent.register_tool(bash_tool)

    # 6. Start the interactive chat loop
    # Or, for programmatic use:
    # response = agent.process_message("What files are in the current directory?")
    # print(f"Agent: {response}")
    agent.chat_loop()

if __name__ == "__main__":
    main()

Core Concepts

LLMAgent: The main orchestrator. It manages the conversation with an LLM, handles tool calls, and invokes tool implementations.

Tool: Represents an external capability the LLM can use. It consists of:

name: A unique identifier.

description: A natural language description for the LLM to understand when to use the tool.

input_schema: A JSON schema defining the parameters the tool expects.

implementation: The Python function that executes the tool's logic.

LLMProvider: An abstraction for different LLM backends (e.g., AnthropicProvider). This allows the agent to be LLM-agnostic.

Developing Custom Tools

Write a Python function that performs the desired action. This function will be the tool's implementation. It should accept arguments as defined in your tool's input_schema and return a string or a serializable dictionary.

Define an input_schema (JSON schema) that describes the parameters your function takes.

Create a Tool object providing the name, description, schema, and implementation function.

Register the Tool with your LLMAgent instance using agent.register_tool(your_tool).

Contributing

Contributions are welcome! Please feel free to open an issue to discuss a new feature or bug, or submit a pull request.

(Consider adding a CONTRIBUTING.md file with more detailed guidelines if the project grows).

License

This project is licensed under the MIT License - see the LICENSE file for details.

Before you commit these files:

  1. Replace placeholders in LICENSE:
    • [Year] with the current year.
    • [Your Full Name or Organization Name] with your name or org name.
  2. Review placeholders in README.md:
    • The build status badge URL (yourusername/llm-tool-harness) should be updated if you set up CI on GitHub Actions or another service.
    • Check the pip install name if you decide to call your package something other than llm-tool-harness on PyPI.
    • Update model names in the example if newer ones become standard (e.g., "claude-3-sonnet-20240229" is current but models evolve).

Metadata

Release files for llm-tool-harness 0.1.1

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

Source distribution (sdist)

Source distribution for llm-tool-harness 0.1.1
File Size Uploaded Provenance
llm_tool_harness-0.1.1.tar.gz 13.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-tool-harness 0.1.1
File Interpreter ABI Platform
llm_tool_harness-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 26.6 kB

Release files / llm_tool_harness-0.1.1.tar.gz

Download URL llm_tool_harness-0.1.1.tar.gz
Size 13.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b5441eb816f2255d483ff8468de0015b20a162ec3847ebf0a33c20a531d4187a
BLAKE2b-256 checksum
How to use checksums
7f85a75f210a9b006fce6221cbc94fe3daf9f810985936a3b219edd9159ae91b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Release files / llm_tool_harness-0.1.1-py3-none-any.whl

Download URL llm_tool_harness-0.1.1-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d59afd9f8291f5cbf91ef7ab2b27856c213851e7d29d7667d0860f6774f4710
BLAKE2b-256 checksum
How to use checksums
8751b1c0476edee87f5f095b483c3696ad1ca8a17d9b474a13040079cae361e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.3

Security for llm-tool-harness 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

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