Skip to main content

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 agents — Agent 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 — sync and async callbacks for tool calls, retries, successes, and final responses
  • Validation hook — supply on_validate to run custom response logic and trigger retries or abort
  • 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

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:

{
  "response": {
    "type": "tool_call",
    "message": "<why you're calling the tool>",
    "tool_call": { "name": "my_tool", "params": [{ "name": "x", "value": "42" }] }
  }
}
{
  "response": {
    "type": "final",
    "message": "<your answer or result of your operation(s)>"
  }
}

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

Metadata

Release files for pyagentkit 1.1.0

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

Source distribution (sdist)

Source distribution for pyagentkit 1.1.0
File Size Uploaded
pyagentkit-1.1.0.tar.gz 55.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyagentkit 1.1.0
File Interpreter ABI Platform
pyagentkit-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 80.7 kB

Release files / pyagentkit-1.1.0.tar.gz

Download URL pyagentkit-1.1.0.tar.gz
Size 55.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c1f184a9f0b9f8588cb10ab7c4103a0d06a48dee8e490700f5be13efcda1bb85
BLAKE2b-256 checksum
How to use checksums
69daa9226ef332b0658eb75e2dded5d709cce1a2435a4b022076b9e9cd291cac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 12, 2026.

Transparency log

Release files / pyagentkit-1.1.0-py3-none-any.whl

Download URL pyagentkit-1.1.0-py3-none-any.whl
Size 25.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e11c350bd5ae3a7d7c6b0b9f55305760a9543f70623a191bec2de3553cd17a5
BLAKE2b-256 checksum
How to use checksums
4ed2e1240b53684457fcd99b249ff717b5ebc7e6c28f2ca4e420da2dbac6d36e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 12, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

0.1.6

2 release files

0.1.5

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