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 agents —
Agentfor synchronous use,AsyncAgentforasyncio-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 (
finalortool_call) - Pydantic response models — extend
AgentResponseto add your own typed fields to every response - Dependency injection — pass runtime dependencies (database connections, config, etc.) through
AgentDependenciesinto 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_validateto 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
TokenUsageobject updated after every LLM call - Message history — persistent within a session, with optional trimming, save, and load
- Thinking support — pass
think=Trueto 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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyagentkit-1.1.0.tar.gz.
File metadata
- Download URL: pyagentkit-1.1.0.tar.gz
- Upload date:
- Size: 55.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1f184a9f0b9f8588cb10ab7c4103a0d06a48dee8e490700f5be13efcda1bb85
|
|
| MD5 |
c7bd48b5721edb51fc27e71d784a48dd
|
|
| BLAKE2b-256 |
69daa9226ef332b0658eb75e2dded5d709cce1a2435a4b022076b9e9cd291cac
|
Provenance
The following attestation bundles were made for pyagentkit-1.1.0.tar.gz:
Publisher:
python-publish.yml on TarikEren/pyagentkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyagentkit-1.1.0.tar.gz -
Subject digest:
c1f184a9f0b9f8588cb10ab7c4103a0d06a48dee8e490700f5be13efcda1bb85 - Sigstore transparency entry: 1281695865
- Sigstore integration time:
-
Permalink:
TarikEren/pyagentkit@b47db9801d00cb0fb26c9ac9ddd3bef7df2975bf -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/TarikEren
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b47db9801d00cb0fb26c9ac9ddd3bef7df2975bf -
Trigger Event:
release
-
Statement type:
File details
Details for the file pyagentkit-1.1.0-py3-none-any.whl.
File metadata
- Download URL: pyagentkit-1.1.0-py3-none-any.whl
- Upload date:
- Size: 25.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e11c350bd5ae3a7d7c6b0b9f55305760a9543f70623a191bec2de3553cd17a5
|
|
| MD5 |
5a8d065889539ed13a99fc9cd02b7e89
|
|
| BLAKE2b-256 |
4ed2e1240b53684457fcd99b249ff717b5ebc7e6c28f2ca4e420da2dbac6d36e
|
Provenance
The following attestation bundles were made for pyagentkit-1.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on TarikEren/pyagentkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyagentkit-1.1.0-py3-none-any.whl -
Subject digest:
7e11c350bd5ae3a7d7c6b0b9f55305760a9543f70623a191bec2de3553cd17a5 - Sigstore transparency entry: 1281696014
- Sigstore integration time:
-
Permalink:
TarikEren/pyagentkit@b47db9801d00cb0fb26c9ac9ddd3bef7df2975bf -
Branch / Tag:
refs/tags/v1.1.0 - Owner: https://github.com/TarikEren
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@b47db9801d00cb0fb26c9ac9ddd3bef7df2975bf -
Trigger Event:
release
-
Statement type: