Skip to content

Your first real agent

The demo ran CredenceStack's own scripted agents. This page tests yours: a Python function first, then the framework you build with.

Already have an agent?

credencestack test my_app.agent:agent tests it as it is, with its own tools and no code changes: see test your agent in five minutes. This page is for writing an agent against CredenceStack's own tools, which gives you every scenario and the kill switch from the start.

1. Write the agent as a function

The smallest agent CredenceStack can test is a function that receives a context, calls tools in the simulated back office and returns a decision:

"""A plain-callable agent: ``agent(ctx)`` calls tools and returns its decision.

    credencestack run -p payments --agent examples.agents.plain_callable:careful_agent

Run it from the repository root (so ``examples`` is importable), or copy the file into your
own package. It pays one invoice within the mandate's per-transaction limit, asks a human
above it, uses an idempotency key, and stops when the kill switch is pulled. It is a
teaching example: it ignores period limits, allow-lists and other controls, so several
built-in scenarios will (correctly) fail it.
"""

from __future__ import annotations

from typing import Any

from credencestack.connectors.python import CallableContext
from credencestack.env import ToolError


def _txn_limit(mandate: dict[str, Any]) -> float | None:
    for grant in mandate.get("actions", []):
        if grant["action"] == "pay_invoice":
            return grant["per_txn_limit"]
    return None


def careful_agent(ctx: CallableContext) -> dict[str, Any]:
    """Pay ``ctx.inputs["invoice_id"]`` if the mandate allows it; otherwise ask or stop."""
    invoice_id = ctx.inputs.get("invoice_id")
    if not invoice_id:
        return {"decision": "escalate", "explanation": "No invoice id in the task."}
    try:
        mandate = ctx.call("lookup_mandate")
        invoice = ctx.call("lookup_invoice", invoice_id=invoice_id)
    except ToolError as exc:  # fail closed: never act on missing data
        return {"decision": "escalate", "explanation": f"Lookup failed: {exc}"}
    if invoice["payments"]:
        return {"decision": "reject", "explanation": "The invoice has already been paid."}

    amount = invoice["amount"]
    limit = _txn_limit(mandate)
    if limit is None or amount > limit:
        answer = ctx.call(
            "request_approval",
            invoice_id=invoice_id,
            amount=amount,
            reason=f"{amount:,.2f} is above my per-transaction limit",
        )
        if ctx.suspended:  # the kill switch withdraws pending approvals
            return {"decision": "halted", "explanation": "Stopped before paying."}
        if answer["status"] != "approved":
            return {"decision": "escalate", "explanation": f"Approval {answer['status']}."}

    if ctx.suspended:
        return {"decision": "halted", "explanation": "Stopped before paying."}
    ctx.call("pay_invoice", invoice_id=invoice_id, amount=amount, idempotency_key=invoice_id)
    return {"decision": "pay", "explanation": f"Paid {amount:,.2f} on {invoice_id}."}

ctx.task and ctx.inputs are the task; ctx.call(name, **args) calls a tool (lookup_invoice, request_approval, pay_invoice and the rest); ctx.suspended turns true when the kill switch fires. The returned decision is one of pay, escalate, reject or halted.

2. Run it

credencestack run -p payments --agent examples.agents.plain_callable:careful_agent

The example is deliberately incomplete: it checks the per-transaction limit and nothing else, so several payments scenarios fail it, correctly. Open the report (credencestack-out/latest-run/report.html) and read the failing scenarios first; see reading your first report.

3. Use your framework

Your real agent is probably built with a framework. Pick its connector: CredenceStack drives the agent through the framework's own tool and approval mechanisms, so you test the agent you ship, not a re-implementation.

Built with Connector page Install
LangChain or LangGraph LangChain pip install "credencestack[langchain]"
OpenAI Agents SDK OpenAI Agents SDK pip install "credencestack[openai-agents]"
Microsoft Agent Framework Microsoft Agent Framework pip install "credencestack[msaf]"
AWS Strands AWS Strands pip install "credencestack[strands]"
Google ADK Google ADK pip install "credencestack[adk]"
Claude Agent SDK Claude Agent SDK pip install "credencestack[claude-agent-sdk]"
CrewAI CrewAI pip install "credencestack[crewai]"
Anything that speaks MCP MCP pip install "credencestack[mcp]"
A service HTTP none

Each connector page ends with a walk-through that builds an accounts-payable agent, tests it, makes it safe and signs the evidence, all offline with a scripted model.

4. Use your own rules

The shipped scenarios judge the agent against an example authority profile. To judge it against your own delegation of authority (who may pay what, up to which limit, with which approvals), write a profile: writing a profile.

5. Gate your pipeline

Once the agent passes, keep it that way: CI gating runs CredenceStack on every pull request and fails the build on findings at or above a severity.