Beta

Python SDK

Install the stigmer package and call the Stigmer API from Python.

The stigmer package is the official Python client for the Stigmer API. It provides typed resource clients, structured error handling, and server-streaming support over gRPC.

Installation

pip install stigmer-sdk

Create a client

Authenticate with an API key from an environment variable:

import os

from stigmer import StigmerClient

client = StigmerClient(os.environ["STIGMER_API_KEY"])

In a CI job or a script, use a service account's key, not a person's: see SDK Overview > Automation.

The client also supports the context manager protocol, which closes the connection when the block exits:

import os
from stigmer import StigmerClient

with StigmerClient(os.environ["STIGMER_API_KEY"]) as client:
    agent = client.agents.get("agent-id")

The client connects to api.stigmer.ai:443 by default; override with the base_url parameter. See SDK Overview > Authentication for authentication methods and SDK Overview > Configuration for all options.

Run an agent

This program connects to the Stigmer running on your machine, sets up a small agent, asks it one thing and prints its answer. Start Stigmer first with stigmer up and give it a model key (Start Stigmer and add your model key); without a key the server refuses the message, and the program prints why.

"""The Stigmer quick start: connect to the Stigmer running on this machine,
set up a small agent, ask it one thing and print its answer.

Start Stigmer first with `stigmer up` (given a model key), then:
python quickstart.py
"""

import sys

from stigmer import AgentInput, ResourceRef, SessionSpecInput, StigmerClient, StigmerError

try:
    # `stigmer up` serves on localhost:7234 and needs no key on your machine.
    with StigmerClient(base_url="localhost:7234", insecure=True) as client:
        # apply creates the agent, or updates it when you run this again.
        agent = client.agents.apply(
            AgentInput(
                name="greeter",
                org="stigmer",
                instructions="You greet people in one short, friendly sentence.",
            )
        )

        turn = client.send_and_wait(
            org="stigmer",
            session_spec=SessionSpecInput(agent_ref=ResourceRef(org="stigmer", slug=agent.metadata.slug)),
            message="Say hello to a developer trying Stigmer for the first time.",
        )
except StigmerError as err:
    sys.exit(f"stigmer: {err}")

if turn.phase != "completed":
    sys.exit(f"the turn did not complete ({turn.phase}): {turn.error}")
print(turn.answer)

send_and_wait sends the message and waits for the agent's answer, reconnecting if the stream drops. A turn that waits for a person's approval returns with phase "waiting" and its asks; answer them with client.sessions.confirm_tool_use, then call client.wait_for_turn with the outcome's last_seq.

On Stigmer Cloud, connect with StigmerClient(os.environ["STIGMER_API_KEY"]) (keys read stk_…), and use your own organization where the program says "stigmer".

Make your first call

Every resource has its own client with methods like get, create, list, apply, update, rename, and delete (Update and apply). Python uses plural snake_case attributes:

from stigmer import StigmerError, is_not_found

try:
    agent = client.agents.get(agent_id)
    print(agent.metadata.name)
except StigmerError as err:
    if is_not_found(err):
        print("Agent does not exist")
    else:
        raise

Act as one of your users

When your backend starts work that belongs to a person signed in to your product, build a second client on a token minted for them, kept in a token cache. What that client starts (a Session, a run, an upload) is the person's. The service account's client mints; the cache mints again only shortly before the token expires.

import os

from stigmer import StigmerClient, TokenCache

admin = StigmerClient(os.environ["STIGMER_API_KEY"])
tokens = TokenCache(lambda: admin.tokens.mint_user_token(org="acme", user_id=user_id))
as_user = StigmerClient(get_access_token=tokens.get_access_token)

The whole flow, and what the person's role allows, is in From your backend, as the user.

What's next