A lightweight async framework for structuring and running AI agents in Python
pip install pygents==0.7.0 Copy PIP instructions
import asyncio from pygents import Agent, Turn, tool @tool() async def greet(name: str) -> str: return f"Hello, {name}!" async def main(): agent = Agent("greeter", "Greets people", [greet]) # Use kwargs: await agent.put(Turn("greet", kwargs={"name": "World"})) # Or positional args: await agent.put(Turn("greet", args=["World"])) async for turn, value in agent.run(): print(value) # "Hello, World!" asyncio.run(main()) Tools are async functions. Turns say which tool to run and with what args. Agents process a queue of turns and stream results. The loop exits when the queue is empty.
Agent/Turn hook boundary — TurnHook covers events fired by the Turn itself (BEFORE_RUN, AFTER_RUN, ON_TIMEOUT, ON_ERROR, ON_COMPLETE). AgentHook covers agent-loop events (BEFORE_TURN, AFTER_TURN, ON_TURN_VALUE, BEFORE_PUT, AFTER_PUT, ON_PAUSE, ON_RESUME). ON_TURN_VALUE stays on Agent because it fires after routing (agent logic). Turn-lifecycle hooks can be registered on an agent via agent.turn_hooks (or the @agent.on_error / @agent.on_timeout / @agent.on_complete decorators) and are automatically propagated to every turn the agent runs.
Hook attachment style — Hooks are attached via method decorators on the instance (@agent.before_turn, @turn.on_complete, @my_tool.before_invoke) rather than constructor parameters. This keeps the API surface explicit and enables IDE autocompletion of hook signatures.
Subtools — Subtools are normal registered tools (in ToolRegistry) that are also attached to a parent for hierarchical documentation. Use @my_tool.subtool() to register a subtool; use doc_tree() on any tool to get a recursive structure of name, description, and subtools (no runtime timing). Registry keys are scoped to the parent (e.g. manage_users.create_user) so different parents can have subtools with the same short name; use that scoped name in turns and lookups. Agents given a root tool accept turns for that tool and all its subtools.
Tool call arguments — Invoking a tool with extra positional or keyword arguments does not raise; only the parameters accepted by the tool function are forwarded. Missing required parameters still raise TypeError when the underlying function is called. This allows callers (e.g. agents or external systems) to pass a superset of arguments without errors.
Tool schemas from type hints — Every decorated tool automatically derives metadata.input_schema and metadata.output_schema from its type hints. Schemas are best-effort JSON-schema-like dicts for common Python typing constructs (primitives, optionals, lists, dictionaries, unions, async generators) and are intended for documentation, UI generation, and validation in higher-level systems. If your project uses Pydantic, any parameter or return type that is a Pydantic model class is detected via its model_json_schema() / schema() method and that model schema is used directly, without pygents depending on Pydantic itself.
Full documentation: uv run mkdocs serve. MkDocs is an optional dependency—install with pip install -e ".[docs]" (or use uv run as above) so the library itself does not depend on it.
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.




