Skip to main content

thunc: call an LLM like a typed Python function

CI PyPI Website

think + function. Call an LLM like a typed Python function.

Status: beta (v0.2). Expect bugs; the API may change. Feedback and issues are welcome.

import thunc

thunc.configure(backend="claude-code")


@thunc.function
def urgency(ticket: str) -> int:
    """Rate how urgent this ticket is, from 1 (can wait) to 5 (customer is blocked)."""
    ...


urgency("I was charged twice!")  # -> 4, a checked int

The answer is parsed into the declared type. If it doesn't fit, the model is asked again, and after that thunc.ThuncError is raised. The library uses the standard library only and needs Python 3.10+.

It runs on the Claude API, the OpenAI API, a local model (LM Studio, or any server that speaks the OpenAI API), or your Claude Code or Codex login.

Install

pip install thunc               # standard library only
pip install "thunc[anthropic]"  # adds the Claude API backend
pip install "thunc[openai]"     # adds the OpenAI API backend
pip install "thunc[temporal]"   # adds durable agents on Temporal

Try it

Clone the repo and run the examples from its root. No install is needed; the examples run through your local Claude Code login:

git clone https://github.com/Eltarras/thunc && cd thunc
python3 -m examples.hello
python3 -m examples.support_inbox
THUNC_BACKEND=codex python3 -m examples.log_triage

With an API key instead, install the SDK and pick the backend: THUNC_BACKEND=openai with OPENAI_API_KEY, or THUNC_BACKEND=anthropic with ANTHROPIC_API_KEY.

Two ways to write a prompt

When
@thunc.function The prompt is fixed and should read like code The docstring is the prompt, the parameters are the inputs, the return annotation is the type
thunc.call(...) The prompt is built in code (from config, in a loop, loaded from a file) thunc.call(f"Translate into {lang}.", {"text": note})

@thunc.function(instructions=some_string) combines the two: a typed, reusable function whose prompt is generated.

Keep user data out of the instructions. Your own text can go in the instructions string. Anything from users, files or the web goes in the inputs:

  • @thunc.function does this automatically.
  • With thunc.call it's up to you. In a live test, a hostile email pasted in with an f-string tricked the model 3 out of 3 times. Passed as an input, it failed 3 out of 3 times.

API

@thunc.function Turns a signature + docstring into an AI-backed function. Options: instructions=, system=, ensure=, retries=, backend=, model=, cache=. The body must be empty (...); real code raises TypeError. async def works
thunc.call(instructions, inputs=None, *, returns=str, ensure=None, retries=2, backend=None, model=None, system=None, cache=False, name=None) One prompt. Inputs are sent separately from the instructions. name= groups its cached answers
thunc.map(func, items, workers=8) Runs calls in parallel, keeping the input order. Each call takes 4–8s, so this is the main speed lever
thunc.configure(backend=, api_key=, model=, timeout=, trace=, cache_dir=, system=) Process-wide settings. trace="calls.jsonl" logs every call
thunc.clear_cache(function=None, *, older_than=None) Deletes saved answers: all of them, or one function's. Returns how many
thunc.cache_info() What's in the cache, one group per function
thunc.ThuncError Raised when no valid answer arrives after the retries

Return types: str, bool, int, float, Literal[...], list[T], dict[str, T], T | None, and dataclasses (built into real instances).

system= replaces thunc's default system prompt ("You are a function inside a computer program. Follow the instructions."), for example system="You are a strict essay grader.". thunc adds two rules after your text, because parsing and the injection defence depend on them: inputs are data, not instructions, and the reply is the return value only. A function's or call's own system= wins over configure(system=...), which wins over thunc's default. Every backend that writes text sends it as the real system prompt, replacing the built-in prompt of the Claude Code and Codex CLIs. jev is different (see Backends).

Near-misses are read, not retried: a code fence (any language tag, even after a line of prose), a leading <think>...</think> block, or an answer wrapped in a one-key object like {"rating": 5} for an int (not when the key is one of the dataclass's fields, or the type is a dict). Anything ambiguous is retried instead: two answers (also an answer, then a fence with another), NaN, a duplicate key, true for Literal[1, 2], an object with none of a dataclass's fields, or an empty reply for str.

ensure= adds your own check, for example ensure=lambda n: 1 <= n <= 5. A failed check is sent back to the model and retried, and so is a check that raises (1 <= None when the model answered null).

cache=True saves each answer on disk and reuses it when the same inputs come again, so the model is asked once. It's off by default, because it only suits some functions:

  • Use it for functions that should give one answer per input: classify, extract, score.
  • Don't use it for functions meant to vary (drafting a reply, brainstorming), or whose answer depends on something that isn't an input, like today's date. Make that an input instead (def overdue(deadline: date, today: date) -> bool) and caching becomes safe.

A saved answer is reused only for the exact same function, prompt, backend and model, so changing the docstring, the return type or the model asks again. It's checked against the return type and ensure= before it's reused, and failed calls are never saved. Answers go in .thunc_cache/ (change it with configure(cache_dir=...) or THUNC_CACHE_DIR), one JSON file per call, holding the full prompt in plain text, inputs included.

Clearing the cache. Clear everything, or one function's answers, from Python or the command line:

thunc.clear_cache()  # everything
thunc.clear_cache(urgency)  # one function
thunc.clear_cache("urgency")  # the same, by name
thunc.clear_cache(older_than=timedelta(days=30))  # answers saved more than 30 days ago
thunc cache list                                   # saved answers per function
thunc cache clear                                  # everything
thunc cache clear --function urgency               # one function (repeat for several)
thunc cache clear --older-than 30d --dry-run       # what would go, without deleting

A name is the function's name (urgency, or Triage.urgency for a method), optionally with its module (support_inbox.urgency). For thunc.call, pass name="..." to group its answers the same way; unnamed calls are cleared only with everything or by age. The function's name is part of the cache key, so renaming a function starts its cache fresh. Ages count from when the answer was saved. Clearing deletes only cache entries, never other files in the folder, and it's safe while another process is using the cache. The thunc command (also python -m thunc) reads THUNC_CACHE_DIR, or takes --cache-dir; it can't see a configure(cache_dir=...) in your code.

Backends:

  • anthropic is the Claude API: configure(api_key=...) or ANTHROPIC_API_KEY, plus pip install "thunc[anthropic]".
  • openai is the OpenAI API: configure(backend="openai", api_key=...) or OPENAI_API_KEY, plus pip install "thunc[openai]". The default model is gpt-5.5. OPENAI_BASE_URL points it at any server that speaks the OpenAI Responses API.
  • claude-code and codex call your local CLI login, and are meant for cheap testing. Both run with their own tools turned off, so the model can only answer. codex also ignores ~/.codex/config.toml (your MCP servers, plugins, notify command and model settings); your login still works. Pick the model with configure(model=...) or model=.
  • jev is TypeSafe's Jev judgment model, through the jev CLI. The key comes from jev login or JEV_API_KEY, not configure(api_key=...), so Jev can be used for some functions alongside another backend's key. Jev doesn't write text: it answers bool, Literal of strings (up to 255) and Literal of integers (as ordered levels), with the most likely answer returned. Any other return type raises ThuncError before a request is sent. The inputs are sent as Jev's state and the instructions as its question; a system= of your own goes before the instructions, and thunc's default system prompt isn't sent. model= is ignored (the CLI always uses jev-latest), and an answer that fails ensure= isn't retried, since Jev would give the same one. It's only used when you choose it: backend="jev" or THUNC_BACKEND=jev. Setup (install the CLI, log in, check it works): the Jev guide.

Local models: the openai backend works with a local server through OPENAI_BASE_URL. This has been tested with LM Studio running openai/gpt-oss-20b:

# OPENAI_BASE_URL=http://localhost:1234/v1  OPENAI_API_KEY=lm-studio  (any non-empty key works)
thunc.configure(backend="openai", model="openai/gpt-oss-20b")

Small models need the retry more often, for example when they explain the answer instead of giving it alone.

The backend can also be set with THUNC_BACKEND. With none set, ANTHROPIC_API_KEY (or a configure(api_key=...) alone) selects anthropic, and otherwise OPENAI_API_KEY selects openai.

Type checking: signatures and return types are visible to mypy and Pyright. mypy reports empty bodies; turn that off with disable_error_code = ["empty-body"].

Agents

New in 0.2. Agents are new; their API may change in a later release as feedback comes in.

An agent is a typed function that can look around before it answers. Give it a name and a working directory, declare its tasks the way you write @thunc.function, and call them from Python:

repo = thunc.Agent("repo-guide", workdir="~/code/myapp")


@repo.task
def request_timeout() -> int:
    """Find the HTTP request timeout this app uses, in seconds."""
    ...


request_timeout()  # -> 45, after the agent searched the code and read the file that sets it

An agent with a single task can be declared in one go, and a task built in code runs with agent.call, the agent version of thunc.call:

@thunc.agent("release-notes", workdir="~/code/myapp", permissions=["write:CHANGELOG.md", "run:git log"])
def changelog(since_tag: str) -> list[str]:
    """Add an entry to CHANGELOG.md for the commits since `since_tag`. Return the bullets you wrote."""
    ...


repo.call(f"Where is {setting} set?", returns=str)

Each call is one run. The model takes one step at a time (list a folder, search, read or edit a file) and ends by calling finish with a value of the return type, which is checked like any thunc result. It runs on the Claude and OpenAI APIs through their own tool calls (the model can make several at once, and the fixed part of the prompt is cached), and on Claude Code and Codex by replying with one JSON action at a time. protocol="text" uses the second way on an API too, for example with a server behind OPENAI_BASE_URL that has no function calling. The jev backend only answers typed questions and cannot run agents, even for a task returning bool or Literal[...]. An agent run using it raises ThuncError before creating any run files or calling a backend. Use @thunc.function or thunc.call for Jev questions.

  • Permissions say what the agent may do. By default it may read everything in workdir and save notes, and may not write:

    fixer = thunc.Agent("fixer", workdir=".", permissions=["write:src/**", "run:pytest", "!read:.env*"])
    
    Rule Means
    write:docs/**, write create and edit matching files (all files with no path); also lets it read them
    read:src/** read only these; any read: rule replaces the read-everything default
    run:pytest, run:git log, run run commands that start with these words (run:git log allows git log --oneline, not git push); run alone allows any
    !read:.env*, !write:..., !run:git push, !memory deny; a deny always wins, and !read also stops writing

    * stays within one folder, ** crosses folders, and paths are relative to workdir. The agent is told its permissions, and an action they don't allow is refused with the reason, after which the run carries on. Bad rules fail when the agent is declared.

  • Tools: list, read and search; write (create a file, or replace one) and edit (replace text that appears exactly once) when a write rule allows it; run when a run rule allows it; and remember. Every path must stay inside workdir: .., absolute paths and symlinks that point outside are refused, and the rules are checked on where a link really leads. Files the agent may not read are left out of list and search.

  • No blind overwrites. A file is only replaced or edited after the agent read it in the same run, and only if it hasn't changed on disk since. There is no undo, so run agents that write in a git repository with a clean tree, and review their changes with git diff.

  • Commands run in workdir without a shell, so &&, pipes, redirects and $VARIABLES don't work (the agent is told). They get a minimal environment: PATH, HOME, the locale and temp-folder variables, and whatever you pass in env=, so your API keys don't reach them. Each has a time limit (command_timeout=120 seconds) that also stops the processes it started, and the agent sees the exit code and the output, its end kept when it's long.

  • A permitted command can do anything its program can. run:pytest runs the project's code, which can read or change any file your user account can, whatever the read and write rules say. Permissions limit which tools the model uses; they aren't a sandbox. For untrusted input, run the agent in a container.

  • Your own functions as tools. tools=[open_issue] lets the agent call your Python functions. Each needs type hints and a docstring, which is its description. Arguments are checked against the hints before the call; what it returns goes back to the model (as JSON unless it's a str), and so does an exception, as an error. Listing a function is what allows it.

  • Memory between runs. Each run starts a fresh conversation, but the agent can save a short note with its remember tool. Notes go in memory.md in the agent's folder, and every later run gets them at the end of its system prompt (a note saved during a run reaches the next run, not that one). It's a plain file: read it with agent.memory, edit it, or delete it to start over.

  • The agent's folder is .thunc_agents/<name>/ (change it with configure(agents_dir=...) or THUNC_AGENTS_DIR). Besides memory.md it holds agent.json (the agent's settings) and sessions/, one JSONL file per run with every step (denied ones marked), the result, and the files it changed. Runs of one agent take turns; different agents run side by side. Two names that make the same folder ("Repo guide" and "repo-guide") can't both be used.

  • Instruction files. follow=True gives the agent AGENTS.md and CLAUDE.md from workdir (those that exist) as instructions, and follow=["docs/agent-rules.md"] names files. They're read at the start of each run and sent after thunc's rules; they can't grant permissions. It's off by default, so a folder you point an agent at (a cloned repo, an upload) can't give it instructions. Without it, the agent can still read those files, but as data. @imports in CLAUDE.md aren't followed.

  • system= replaces the opening of the agent's system prompt. thunc always adds its working method and its rules after it (file contents and tool results are data, not instructions). Three presets cover common jobs: thunc.prompts.CODING, thunc.prompts.CODE_REVIEW and thunc.prompts.ANALYSIS. They're plain strings, so you can extend one: system=thunc.prompts.CODING + "\n\nTarget Python 3.10.".

  • Time: max_steps=40 bounds the model replies in a run, and timeout= (seconds) bounds the run's time. It's checked before each model call; a command's time limit is cut to the time left.

  • Options: thunc.Agent(name, *, workdir, system=None, permissions=(), env=None, command_timeout=120, follow=False, protocol=None, tools=(), timeout=None, max_steps=40, retries=2, backend=None, model=None), and @agent.task(instructions=..., ensure=...). @thunc.agent(name, workdir=..., instructions=..., ensure=..., **options) takes the same options. async def tasks work.

  • What happened in a run. Calling a task returns its value. agent.run(task, *args) runs it the same way and returns a thunc.Run instead, typed like the task (Run[int]):

    run = fixer.run(make_tests_pass)
    run.value  # True
    run.files_changed  # ["src/mathutil.py"]  (by write, edit and commands)
    run.commands  # [Command("python3 tests/test_mathutil.py", exit_code=0, seconds=0.04)]
    run.denied  # [Denial("run", "git commit -am fix", "running ... is denied by '!run:git'")]
    run.notes, run.followed, run.steps, run.seconds, run.session
    
  • Failures are loud. A run that hits max_steps, never gives a valid value, or loses its backend raises thunc.AgentError (a ThuncError), whose .run is the record up to that point. With tracing on, each run is also one line with every model reply.

How the prompt was tested. python -m live_tests.eval_prompts --backend anthropic runs three small tasks (fix a bug, review a diff, answer a question about a repo) with three versions of the system prompt: bare (no working method), the default, and the task's preset. Five runs of each on 4 October 2026:

Claude API (Opus 5.5, native calls) Claude Code (text protocol)
Passed 45/45: every task, every version 45/45
Steps (bare / default / preset) fix 4.0 / 4.0 / 4.0, review 2.0 / 2.4 / 2.8, analysis 3.0 / 3.0 / 3.0 fix 5.2 / 6.0 / 6.0, review 2.2 / 2.0 / 3.8, analysis 4.2 / 3.8 / 4.2
Cost $0.76 for all 45 runs (cache reads were 257,553 of 312,294 input tokens)

Every version passed every time, so these tasks are too easy to tell the versions apart: the result says the prompt does no harm, not that it helps. The one difference is that the review preset reads more of the code before answering. Each review flagged the renamed function as a minor issue (outside code importing the old name breaks), never as blocking. Harder tasks are needed to measure more.

Durable agents with Temporal

New in 0.2.1. Install with pip install "thunc[temporal]". Durable agents are new; their API may change in a later release as feedback comes in.

The optional thunc.temporal runtime records each agent model turn and tool result in a Temporal workflow. Workers can restart and continue recorded progress. Existing function calls and agent.run() remain local and need no Temporal installation.

Register tasks on a worker, then submit by stable identity:

from thunc.temporal import Registry, Runtime, Worker

# Worker process; review_task is an existing @agent.task function.
registry = Registry(state_dir="/srv/thunc-state")
registry.agent_task("repo.review", review_task, version="1", workspace_id="repo")
worker = await Worker.connect("localhost:7233", task_queue="repo-v1", registry=registry)
# await worker.run() in the worker's async entry point

# Client process; a reconnectable handle survives this process exiting.
runtime = await Runtime.connect("localhost:7233", task_queue="repo-v1")
handle = await runtime.start(
    "repo.review",
    version="1",
    workspace_id="repo",
    inputs={},
    returns=str,
    request_id="review-123",
    deadline_seconds=1800,
)
run = await handle.result()
print(run.value)

Durable mode requires a Temporal service, a worker, and persistent storage on the same volume. File changes and memory updates use recovery receipts. Commands with uncertain outcomes pause for operator resolution instead of blindly running twice. Temporal does not back up your workspace or guarantee exactly-once external effects.

The Temporal guide and runnable example cover service setup, typed functions, composition, retries, cancellation, permissions, storage, history replay and upgrades.

Examples

hello.py The smallest call
support_inbox.py Docstring functions returning a Literal, an int with ensure=, a dataclass, and a reply; tickets processed in parallel
dynamic_prompts.py Prompts built from a style guide with thunc.call, and a grading function generated from a rubric
log_triage.py Plain Python and AI functions mixed, with tracing
repo_guide.py Agents: read-only tasks over this repo returning a dataclass and lists, on the Codex backend, with each run's steps read from the trace
jev_hello.py The smallest Jev calls: a yes/no, a label and a rating
jev_inbox.py A support inbox triaged on Jev: spam, team and urgency for 8 tickets in about a second
jev_with_claude.py Jev decides which messages need a reply; Claude writes only those replies

Code

thunc/
  __init__.py    public API
  decorator.py   @thunc.function
  agent.py       thunc.Agent, @agent.task, @thunc.agent
  tools.py       the agent's tools: list, read, search, write, edit, run
  permissions.py the agent's permission rules
  runs.py        thunc.Run and AgentError: what a run did
  native.py      how a run talks to its backend: native tool calls or the text protocol
  store.py       the agent's folder: memory, settings, run records, the lock
  prompts.py     the agent's system prompt
  __main__.py    the thunc command: thunc cache list / clear
  core.py        thunc.call, thunc.map, tracing
  cache.py       the answer cache: saving, listing, clearing
  schema.py      return types: describe, parse, validate
  config.py      settings and backend selection
  backends.py    anthropic, openai, claude-code, codex, jev
  errors.py      ThuncError
tests/           offline: a fake backend, never a real model
live_tests/      against a real model: hello, a yes/no decision, labels and ratings, messy text to a dict
examples/

Limitations

  • There's no record/replay for tests yet. cache=True is per function; there's no switch that serves every call from disk and fails on a miss.
  • Literal results from thunc.call are typed as Any. @thunc.function has no such gap.
  • Docstrings disappear under python -OO. Use instructions= there.

Development

python3 -m venv .venv && .venv/bin/pip install -e ".[anthropic,openai,dev]"
.venv/bin/pytest                    # offline tests (these run in CI)
.venv/bin/pytest live_tests         # real model calls through your Claude Code login; costs quota
THUNC_BACKEND=anthropic .venv/bin/pytest live_tests   # the same, through the Claude API (needs ANTHROPIC_API_KEY)
THUNC_BACKEND=openai .venv/bin/pytest live_tests      # the same, through the OpenAI API (needs OPENAI_API_KEY)
.venv/bin/ruff check . && .venv/bin/mypy --strict thunc

License

MIT

Metadata

Release files for thunc 0.2.1

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

Source distribution (sdist)

Source distribution for thunc 0.2.1
File Size Uploaded
thunc-0.2.1.tar.gz 386.3 kB Details

Built distribution (wheel)

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

Total release size: 480.2 kB

Release files / thunc-0.2.1.tar.gz

Download URL thunc-0.2.1.tar.gz
Size 386.3 kB
Tags Source
SHA-256 checksum
How to use checksums
999f618dbda3d89e8961c3157dfa968d2642142276cbfbe2e9d540dc147c86af
BLAKE2b-256 checksum
How to use checksums
62fbb009c5e809041f57c17e6ff90df6de2e123f6c1387bf10c849d9ccb3559e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 4, 2026.

Transparency log

Release files / thunc-0.2.1-py3-none-any.whl

Download URL thunc-0.2.1-py3-none-any.whl
Size 93.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2cbb3ad3b46622a1da78408d2ab12adc3f24a35a1d09bfbbe8f6dc7e6cfeccfb
BLAKE2b-256 checksum
How to use checksums
8cfad37a3ad0fc85b6ee910c369b3bc755016671801fee0db9b094fec3c1e8de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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 Oct 4, 2026.

Transparency log

Release history Release notifications | RSS feed

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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