> ## Documentation Index
> Fetch the complete documentation index at: https://docs.invokehq.run/llms.txt
> Use this file to discover all available pages before exploring further.

# Python SDK

> invokehq: give Invoke an action, and it comes back safe, attributable, observable and verifiable. Install, the one call, its result, and every error.

`invokehq` is the whole integration. It has one method that matters: `invoke.call()`.

```python theme={null}
from invokehq import Invoke

invoke = Invoke.from_env()

done = invoke.call(
    tool="linear.update_issue",
    params={"issue_id": "DRA-8", "state": "Done"},
    agent_id="coding-agent",
    idempotency_key="DRA-8:done",
)

done.receipt.number  # "#e59a65d7": signed proof it happened
```

> [!NOTE]
>
> **The contract.** If `call()` returns, the action happened and you hold its receipt; a repeat of the same call returns that same receipt instead of acting again. If it didn't happen, `call()` raises, and the exception's class tells you whether it happened.

## Install

```bash theme={null}
pip install invokehq
```

Python 3.9+. No dependencies. Typed.

## Get a key

In the [console](https://console.invokehq.run), open **API** and copy the **Agent key**. It belongs to one agent, and every call made with it is recorded as that agent's. The key proves the identity, so you can leave out `agent_id`.

```bash theme={null}
export INVOKE_API_KEY=inv_live_…
```

The **Account key** also works. It can act for any agent registered in your workspace, so name the agent with `agent_id` on each call, or set `INVOKE_AGENT_ID`. Keep keys on the server.

| Variable | |
| - | - |
| `INVOKE_API_KEY` | Required. An agent key (recommended) or your account key. |
| `INVOKE_API_URL` | Default `https://api.invokehq.run` |
| `INVOKE_WORKSPACE_ID` | Default: the one workspace the key can see |
| `INVOKE_AGENT_ID` | Default acting agent, for account keys |

`Invoke.from_env()` reads these. To configure in code instead, use `Invoke(api_key, base_url=..., workspace_id=..., agent_id=..., timeout=60.0, max_retries=2, transport=...)`.

## invoke.call()

| Argument | Type | |
| - | - | - |
| `tool` | `str` | Required. The tool to run, such as `linear.update_issue`. |
| `params` | `dict` | The tool's arguments. |
| `agent_id` | `str` | Who is acting. With an agent key, the key decides. |
| `idempotency_key` | `str` | Names the action. The same key is the same action: it runs at most once. |
| `intent` | `str` | Why the agent is doing it. Recorded on the receipt. |
| `task_id` | `str` | The task the action is for. Paid from the task's budget, and without a key the call is keyed to the task, so the same call again gets the first receipt. |
| `wait_for_approval` | `bool \| float` | Wait for a human instead of raising `ApprovalRequiredError`: `True` for up to 10 minutes, or a number of seconds. |

Before the tool runs, Invoke checks the agent's identity and permissions, your workspace policies, the budget, and whether another agent has claimed the work item. Then it reserves the cost, runs the tool once, settles the cost and seals a receipt.

For async code, `await invoke.acall(tool, params, ...)` takes the same arguments.

### The result

`call()` returns only for an action that happened, as a `CallResult`:

```python theme={null}
done.status        # "committed", or "duplicate": a repeat of a finished action, so nothing ran again
done.duplicate     # True for a repeat
done.output        # what the tool returned
done.receipt       # Receipt(number="#e59a65d7", verified=True, sealed_with="current", raw={...})
done.effect_id     # "eff_e59a65d78a5a4baf941fefaa"
done.agent_id      # "coding-agent"
done.cost_usd      # 0.001
done.duration_ms   # 291
```

`receipt.verified` is `True` when the ledger still matches what was signed, and `receipt.sealed_with` says which key signed it. `receipt.raw` holds the whole receipt: who, why, input, output, cost and approval.

## Idempotency

Name every action that must not happen twice. The same `idempotency_key` means the same action. It could be sent again by your retry loop, a worker that crashed and restarted, or an agent re-running its plan. Either way it runs once, and every repeat gets the original receipt with `status == "duplicate"`.

Build the key from what makes the action unique, such as `f"invoice:{invoice_id}"`, `"DRA-8:done"` or `f"webhook:{event.id}"`. Reusing a key with different params is refused with `idempotency_conflict`, because one key names one action.

Without a key, the SDK makes one for each call, so its own network retries are still safe. But if your code calls again, that's a new action.

## Errors

Every failure is an `InvokeError`. Its class tells you whether the action happened:

| Exception | Did it happen? | What to do |
| - | - | - |
| `DeniedError` | No | `err.decision` names the check that refused it: `denied_permission`, `denied_policy`, `denied_budget`, `denied_identity`, `denied_claim`, `idempotency_conflict`, `unknown_tool` or `unknown_agent`. |
| `ApprovalRequiredError` | Not yet | A policy wants a human. Approve it in the console, or pass `wait_for_approval`. |
| `ToolError` | No | The tool failed and nothing changed. Fix the input and call again. |
| `OutcomeUnknownError` | Maybe | The tool got the request but its answer was lost. See below. |
| `InvokeConnectionError` | Only if it reached Invoke | The SDK already retried with the same key. Call again later with the same `idempotency_key`. It is also a built-in `ConnectionError`. |
| `ConfigError` | No | A setup problem. The message says what to set. |

```python theme={null}
from invokehq import ApprovalRequiredError, DeniedError, OutcomeUnknownError

try:
    invoke.call(tool=tool, params=params, idempotency_key=key)
except DeniedError as err:
    ...  # refused before anything ran; err.decision says which check
except ApprovalRequiredError:
    ...  # waiting for a human; nothing has run yet
except OutcomeUnknownError as err:
    ...  # it may have happened; retry only if err.safe_to_retry
```

### When the outcome is unknown

Sometimes the request reaches the tool and the answer never comes back. Invoke doesn't guess what happened.

* **Tools that are safe to re-run** as the same action, such as reads and `linear.update_issue`, are reconciled for you: the SDK sends the same action again. You only see `OutcomeUnknownError` if that fails too.
* **Anything else**, such as a charge or an email, could act twice if retried. Invoke holds the action as unknown and sets `err.safe_to_retry` to `False`. Someone checks the real system and settles it at `err.resolve_url`, or `invoke.observe(err)` asks Invoke to look at the real system and settle it.

## Approvals

Approvals come from workspace policies. For example, "`stripe.charge_customer` over 5000 needs a human" (created with an account key):

```bash theme={null}
curl -X POST https://api.invokehq.run/v1/workspaces/$WORKSPACE_ID/policies \
  -H "X-API-Key: $ACCOUNT_KEY" -H "Content-Type: application/json" \
  -d '{"name": "big charges need a human", "effect": "require_approval",
       "match": {"tool": "stripe.charge_customer"},
       "conditions": [{"field": "amount", "op": "gt", "value": 5000}]}'
```

A call that matches raises `ApprovalRequiredError`, and nothing runs. To wait for the decision instead:

```python theme={null}
done = invoke.call(
    tool="stripe.charge_customer",
    params={"customer_id": "cus_123", "amount": 50_000, "currency": "usd", "invoice_id": "inv_881"},
    idempotency_key="invoice:inv_881",
    wait_for_approval=300,  # seconds; True waits up to 10 minutes
)
```

Once approved, the action runs and `call()` returns its receipt, which records who approved it. If the reviewer rejects it, `call()` raises `DeniedError`.

## Before and after

```python theme={null}
invoke.preview(tool, params)     # PreviewResult(allowed, verdict, reason): would it run? Runs nothing.
invoke.receipt(done)             # the full receipt
invoke.trace(done)               # the run, stage by stage: agent → identity → task → context →
                                 # policy → budget → action → result → receipt → trace
invoke.attention()               # what's waiting on a human right now, by agent
invoke.agent("billing-agent")    # the same client, acting as another agent (account keys)
```

## Next steps

* [Quick Start](https://docs.invokehq.run/quickstart.md): your first call in five minutes
* [LangGraph](https://docs.invokehq.run/frameworks/langgraph.md), [CrewAI](https://docs.invokehq.run/frameworks/crewai.md) and [OpenAI Agents SDK](https://docs.invokehq.run/frameworks/openai-agents.md): Python agents over MCP
* [Environment Variables](https://docs.invokehq.run/configuration/environment-variables.md): every setting the SDK reads


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.