Agent harness

Agent harness quickstart

Run your own agent loop with Harny in a few lines of Python, on your own model key.

Copy page
View as Markdown

Install

Install the SDK. It needs Python 3.10 or newer.

pip install harny

Run a first agent

Save this as a script. Use your app key, which starts with fsa_. In real code, read it from your environment, for example from HARNY_APP_KEY, instead of writing it in the file.

import asyncio
from harny import HarnyClient
from harny.agent import Agent, AgentConfig, FileSessionStore

async def main():
    async with HarnyClient(api_key="fsa_...") as harny:   # your app key, read from the environment in real code
        agent = await Agent.create(
            harny=harny,
            preset="cost_first",                           # resolved against Harny's model roster
            config=AgentConfig(instructions="You help one person with their calendar."),
            store=FileSessionStore("./data/sessions"),
        )
        result = await agent.run(user="demo-user", message="Put the dentist on Thursday at 3pm")
        print(result.answer, [p.public() for p in result.pending_confirmations])

asyncio.run(main())

The loop runs in your code. The presets are cost_first, quality_first, and regulated.

Model key

Harny never sees your model key. Put it in the provider's conventional variable, for example ANTHROPIC_API_KEY. The SDK reads it from there. You can also pass it as api_key= with provider= in code.

What the result holds

agent.run returns a result with these fields:

  • run_id: the id of this run.
  • answer: the reply text for the person.
  • stopped_reason: one of completed, cut_off, max_steps, spend_cap, timeout, model_error, or blocked.
  • steps and actions: what the agent did during the run.
  • pending_confirmations: actions that wait for a yes.
  • usage, cost_usd, model, and trace_id.

Default limits

A run uses these defaults:

  • actions: ("calendar.create_event",)
  • allow_undo: True
  • max_steps: 6
  • run_timeout_s: 90
  • spend cap: 100,000 tokens

Confirm a pending action

An action that needs a yes comes back in pending_confirmations. Each item is a pending confirmation. Your code approves or refuses it. The model cannot do this.

await agent.confirm(pending, approve=True)   # or approve=False to refuse

Which actions wait for a yes depends on the permission settings of your app. See Permissions.

Where runs show up

Set record_runs=True to send a short summary of each run after it ends. The summary holds the step kind, tool name, outcome, duration, and token counts. It never holds the person's message or the answer text. A failed send does not change the run.

Every action is also recorded. Harny records each one with POST /v1/actions, and you can list and export the records.

Next