Production-grade reusable agent loop SDK — custom ReACT, JSON-mode tool calls, MCP client, pluggable LLM/state/tools.
Project description
fifty-agent-sdk
fifty-agent-sdk is a reusable agent loop for python. it implements a custom reACT loop with json-mode tool calls, an mcp client, and pluggable llm, state, and tool backends. it exists because the loop, the parser, the safety checks, and the runner kept getting rewritten per project. this is that loop, factored out once: write the tools, hand them to the runner, let it iterate.
At a glance
- talks to any openai-compatible chat-completions endpoint by swapping one
base_url: openai, google distributed cloud, a local oss server. - llm clients, state stores, and tools are pluggable behind protocols: bring your own, the loop stays the same.
- the run emits a typed event stream the caller consumes, so you watch the react loop step by step.
- an iteration cap and per-tool timeouts bound every run, with a fallback answer on error or cap: a loop that can't end is a loop that doesn't ship.
- zero-infra by default: no db, no redis, until you opt into an extra.
Installation
pip install fifty-agent-sdk
Optional extras:
pip install 'fifty-agent-sdk[sql]'— enables SqlStateStore, SqlAuditSink, SQLAlchemypip install 'fifty-agent-sdk[redis]'— enables RedisStateStore
Importing fifty_agent_sdk pulls neither extra; the extra symbols are re-exported lazily, and first access without the relevant extra installed raises a clear ImportError. The sql extra installs SQLAlchemy but not a database driver — bring your own async driver (e.g. aiosqlite for SQLite, asyncpg for PostgreSQL).
Requires Python >=3.11.
Quickstart
the example builds a tool, hands it to the AgentRunner, and consumes the typed event stream the run emits.
import asyncio
from typing import Any
from fifty_agent_sdk import (
JSON_MODE_OUTPUT_FORMAT,
AgentLoop,
AgentRunner,
JsonModeParser,
MemoryStateStore,
OpenAICompatibleClient,
PromptSections,
Registry,
SafetyConfig,
tool,
)
@tool()
async def get_weather(city: str) -> dict[str, Any]:
"""Return the current weather for a city."""
return {"city": city, "temp_c": 21}
async def main() -> None:
# 1. An LLM client — points at any OpenAI-compatible endpoint.
# Pass base_url=... to target GDC or a local OSS server instead of OpenAI.
llm = OpenAICompatibleClient(api_key="sk-...")
# 2. A tool registry — register the decorated tool.
registry = Registry()
registry.register(get_weather)
# 3. The ReACT loop — LLM + registry + parser + prompts + safety.
# `output_format` shows the model the JSON envelope the parser
# expects; without it JsonModeParser raises ParserError on every turn.
loop = AgentLoop(
llm=llm,
registry=registry,
parser=JsonModeParser(),
prompts=PromptSections(persona="You are helpful."),
safety=SafetyConfig(),
model="gpt-4o",
output_format=JSON_MODE_OUTPUT_FORMAT,
)
# 4. The runner — wraps the loop with conversation-state persistence.
runner = AgentRunner(
loop=loop,
state=MemoryStateStore(),
system_prompt="You are a helpful weather assistant.",
)
# 5. Drive a turn and consume the event stream.
async for event in runner.run("session-1", "What's the weather in Paris?"):
print(event)
asyncio.run(main())
Core concepts
tools
the registry of functions the agent can call. each tool is a side-effecting action exposed to the loop, so the model can do something in the world and not just talk about it.
llm
the llm client. a protocol plus an openai-compatible adapter, so the loop talks to any chat-completions endpoint by changing one base_url.
state
the state stores. where conversation state persists between turns, with branching built in: fork a session, switch between branches, truncate back to an earlier point. MemoryStateStore needs no infrastructure; SqlStateStore and RedisStateStore are durable backends behind the extras.
streaming
a typed event stream the caller consumes while the loop runs. each step in the run surfaces as an event instead of waiting for a final blob.
safety
the caps that bound a run: a max-iteration ceiling on react cycles and a per-tool timeout, plus the fallback answer returned when a run errors or hits the cap. a loop that can't end is a loop that doesn't ship.
audit
the audit sinks and observability hooks. they record what the agent did, so a run can be read back after it finishes.
Architecture
fifty_agent_sdk — module graph (from src/fifty_agent_sdk/, ground-truth imports)
src/fifty_agent_sdk/
├─ ▢ audit
├─ errors
├─ ▢ llm
├─ loop
├─ ▢ mcp
├─ ▢ observability
├─ ▢ parser
├─ prompts
├─ ▶ runner
├─ safety
├─ ▢ state
├─ streaming
└─ ▢ tools
depends (→):
audit → errors
llm → errors
loop → errors
loop → llm
loop → observability
loop → parser
loop → prompts
loop → safety
loop → streaming
loop → tools
mcp → errors
observability → llm
parser → errors
parser → llm
runner → audit
runner → errors
runner → llm
runner → loop
runner → observability
runner → state
runner → streaming
state → errors
state → llm
streaming → tools
tools → errors
tools → llm
tools → mcp
legend: ▶ entry ▢ package name module → depends
What's new in 1.2.0
- branching — first-class conversation branching on
StateStore:fork,list_branches,switch_branch, branch-scopedget_messages(..., branch_id=...), plusBranchInfoandTRUNK_BRANCH_ID. a session is now a tree of branches with an active head, andappendwrites to the active branch (the edit-a-message / regenerate model). implemented across memory, SQL, and Redis backends, data-additive and zero-migration: existing sessions read as the trunk branch. breaking for customStateStoreimplementations: they must add the new methods. StateStore.truncate_after(session_id, sequence, *, branch_id=None)— a destructive hard-delete of a branch's tail (messages with sequence > N), for redaction, retention, and rollback. only the target branch's own messages are removed (afork's inherited prefix is never touched), and it is idempotent: a no-op on an unknown session or branch.
editing a turn is a consumer-side fork-then-append, and the original line stays reachable:
# Edit a turn = fork the history before it, switch onto the new branch, then
# append the edited message. `store` is any StateStore; import `ChatMessage`
# from fifty_agent_sdk.
branch = await store.fork(session_id, from_sequence=4) # keep messages 1..4
await store.switch_branch(session_id, branch)
await store.append(session_id, ChatMessage(role="user", content="...edited..."))
await store.get_messages(session_id, branch_id="trunk") # original line intact
Links
License
MIT.
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 fifty_agent_sdk-1.3.0.tar.gz.
File metadata
- Download URL: fifty_agent_sdk-1.3.0.tar.gz
- Upload date:
- Size: 123.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
36d06e60e0f87ee83d0ceedc59f418ecc4d00f5de5aee15c330f85e013b4579e
|
|
| MD5 |
acf3eabbd23690aeddb616b8845d7697
|
|
| BLAKE2b-256 |
deeb6bc3363af249003fbb8deecd7e1ef4795e182c608341e6ad9a9925047b0c
|
Provenance
The following attestation bundles were made for fifty_agent_sdk-1.3.0.tar.gz:
Publisher:
release.yml on fiftynotai/fifty-agent-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fifty_agent_sdk-1.3.0.tar.gz -
Subject digest:
36d06e60e0f87ee83d0ceedc59f418ecc4d00f5de5aee15c330f85e013b4579e - Sigstore transparency entry: 2035565572
- Sigstore integration time:
-
Permalink:
fiftynotai/fifty-agent-sdk@68bd4541c49c769544795a092d6b57cef9928fda -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/fiftynotai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@68bd4541c49c769544795a092d6b57cef9928fda -
Trigger Event:
push
-
Statement type:
File details
Details for the file fifty_agent_sdk-1.3.0-py3-none-any.whl.
File metadata
- Download URL: fifty_agent_sdk-1.3.0-py3-none-any.whl
- Upload date:
- Size: 137.3 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 |
b637c52fd7b85ec37ee6b37be7935716cb812363097ef2a669fc604ea3cade9a
|
|
| MD5 |
e6d4e7395bc569ed41fe9cbd4e4f6ed6
|
|
| BLAKE2b-256 |
4f6e09bfeede13c65aff5817307bfeadaf125a2f7bac51b5730b2ab8f69b3dd3
|
Provenance
The following attestation bundles were made for fifty_agent_sdk-1.3.0-py3-none-any.whl:
Publisher:
release.yml on fiftynotai/fifty-agent-sdk
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fifty_agent_sdk-1.3.0-py3-none-any.whl -
Subject digest:
b637c52fd7b85ec37ee6b37be7935716cb812363097ef2a669fc604ea3cade9a - Sigstore transparency entry: 2035565709
- Sigstore integration time:
-
Permalink:
fiftynotai/fifty-agent-sdk@68bd4541c49c769544795a092d6b57cef9928fda -
Branch / Tag:
refs/tags/v1.3.0 - Owner: https://github.com/fiftynotai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@68bd4541c49c769544795a092d6b57cef9928fda -
Trigger Event:
push
-
Statement type: