Skip to main content

Agent toolkit for Ollama

Project description

PyAgentKit

A Python library for building tool-calling agents on top of locally-hosted LLMs via Ollama. PyAgentKit gives models that lack native function-calling support the ability to call tools through structured JSON output, retries, dependency injection, and lifecycle hooks.


Features

  • Sync and async agentsAgent for synchronous use, AsyncAgent for asyncio-based workflows
  • Tool registration — attach tools at the class level (shared across all instances) or at the instance level (per-agent)
  • Structured JSON responses — agents respond in a validated, discriminated-union schema (final or tool_call)
  • Pydantic response models — extend AgentResponse to add your own typed fields to every response
  • Dependency injection — pass runtime dependencies (database connections, config, etc.) through AgentDependencies into tools without polluting tool signatures
  • Retry logic — configurable retry budgets separately for tool calls and for response validation failures
  • Approval gates — optionally require human confirmation before any tool is executed
  • Lifecycle hooks — callbacks for tool calls, retries, successes, and final responses
  • Agent composition — expose any agent as a tool that another agent can call via .as_tool()
  • Token usage tracking — cumulative TokenUsage object updated after every LLM call
  • Message history — persistent within a session, with optional trimming, save, and load
  • Thinking support — pass think=True to enable chain-of-thought reasoning on supported models

Requirements

  • Python 3.12+
  • Ollama running locally (or at a reachable URL)
  • A model pulled in Ollama (e.g. ollama pull llama3.2)

Installation

pip install pyagentkit

Project Structure

src/pyagentkit/
├── agent.py          # Synchronous Agent class
├── async_agent.py    # Asynchronous AsyncAgent class
├── definitions.py    # Pydantic models, type aliases, enums
└── exceptions.py     # Exception hierarchy

Core Concepts

Response Schema

Every agent responds in one of two JSON shapes, validated via Pydantic:

// Tool call
{
  "response": {
    "type": "tool_call",
    "tool_call": { "name": "my_tool", "params": [{ "name": "x", "value": "42" }] }
  },
  "message": "Calling my_tool to get the result"
}

// Final answer
{
  "response": { "type": "final" },
  "message": "The answer is 42"
}

Exception Hierarchy

PyAgentKitError
├── ExceptionAgentError          # Recoverable response failure (triggers retry)
├── ExceptionAgentFatal          # Irrecoverable response failure
├── ExceptionToolError           # Recoverable tool failure (triggers retry)
├── ExceptionToolFatal           # Irrecoverable tool failure
├── ExceptionToolRetriesExhausted
├── ExceptionResponseRetriesExhausted
├── ExceptionEnvironmentError    # Ollama unreachable or model not found
├── ExceptionInvalidTool         # Tool missing docstring or malformed
└── ExceptionFatalError          # Wraps any fatal agent or tool exception
ExceptionUnhandledError          # Unhandled runtime exception (not a PyAgentKitError)

License

APACHE 2.0

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

pyagentkit-0.1.6.tar.gz (54.3 kB view details)

Uploaded Source

Built Distribution

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

pyagentkit-0.1.6-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file pyagentkit-0.1.6.tar.gz.

File metadata

  • Download URL: pyagentkit-0.1.6.tar.gz
  • Upload date:
  • Size: 54.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyagentkit-0.1.6.tar.gz
Algorithm Hash digest
SHA256 09524015907bc3c05591c72b1ffe23336f6025f3ca7088e201b5d2ebc00ee093
MD5 c5e7d2374d9c01a13765ef226347abee
BLAKE2b-256 ded2dbfc00ed746b82891888a59499f455e5da4ad411fee738ceb497700a2391

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyagentkit-0.1.6.tar.gz:

Publisher: python-publish.yml on TarikEren/pyagentkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyagentkit-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: pyagentkit-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyagentkit-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 660cbda2b20169a154c6f5db0361307fc9a8ee420bc1ba4429a87ff5db5e57e8
MD5 90321f468e2c0211aa688194d1eb3fa0
BLAKE2b-256 e1fb4036c3d40241c2831428e14d77993b021b7665472bb93bc279452ca47a07

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyagentkit-0.1.6-py3-none-any.whl:

Publisher: python-publish.yml on TarikEren/pyagentkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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