Agent calls and methods
from openprogram import Agent
class ReviewAgent(Agent):
tools = []
instructions = "Preserve facts. Return concise text."
def classify(self, review: str) -> str:
"""Classify review sentiment."""
return self(f"Classify as positive, negative, or neutral:\n{review}")
def summarize(self, review: str) -> str:
"""Summarize a review with its sentiment."""
sentiment = self.classify(review)
return self(f"Summarize this {sentiment} review in one sentence:\n{review}")
reviewer = ReviewAgent(model="configured-model")
summary = reviewer.summarize("The service was fast.")
self(...) makes a model request through the existing Runtime. The docstring describes the function; the prompt carries instruction and data.
| Thing | Behaviour |
|---|---|
| Agent instance | Stores configuration; keeps no conversation between independent top-level calls |
| Nested call, same Agent | Uses the active execution; no DAG node of its own |
| Another Agent | Its own DAG; the parent stores the result and a child_session_id reference |
| DAG contents | User messages, model requests and directly dispatched tools |
| Permission, cancellation, file checkpoints | Stay with the originating execution |
| Context | Selects permitted history and resolves configured content per request |
Direct and asynchronous calls
from openprogram import agent, agent_async
answer = agent("Summarize this text", tools=[])
# In an async function:
# answer = await agent_async("Summarize this text", tools=[])
# answer = await reviewer.arun("Summarize this text")
tools=[]: no model tools. tools=None: the runtime resolves available tools under current authorization.
Method metadata and tools
class TextAgent(Agent):
tools = []
method_options = {
"summarize": {
"tool": True,
"name": "summarize_notes",
"input": {"text": {"description": "Text to summarize"}},
"expose": "io",
},
}
def summarize(self, text: str) -> str:
"""Summarize text."""
return self(f"Summarize:\n{text}")
method_options declares input form metadata, visibility, durable steps, authorization and tool registration; fields are in Function metadata. Only tool=True makes a tool; recording does not. Name and signature supply the schema. Visibility controls history rendering, not permission.
Ordinary Program functions
The managed loader captures source-defined functions in authorized Program packages, not arbitrary host files or dependencies. List public entries in PROGRAM_ENTRIES; private helpers keep runtime access without DAG nodes or tool status. Compose deterministically in Python, or supply registered tools for model-selected calls.
Persistent conversations
from openprogram import Agent, Context, TurnRequest
from openprogram.runtime.context.session_db import default_db
context = Context.for_session(default_db(), "example-conversation",
blocks={"reference": "User-supplied reference material"})
worker = Agent(context=context)
result = worker.run_turn(
TurnRequest(session_id="example-conversation", user_text="Summarize the reference",
agent_id="main", source="python"),
on_event=lambda event: print(event["type"]),
)
print(result.final_text)
ChatAgent is the conversation subtype behind the App, Web, TUI and channel adapters. It and Agent.run_turn() share one turn runtime for history, permissions, tools, streaming, compression and final persistence.
| Item | Rule |
|---|---|
| Next turn | Reuse the session ID |
Context.for_session | Selects the store; creates no provider. Optional head_id selects the predecessor branch. |
Context(history=None) | Excludes past graph content, keeps the current prompt |
| Named content and providers | Rendered as user content; cannot alter system instructions or tool permissions |
arun_turn(), aresume_turn() | Async versions. Callbacks run on the execution thread, so hand off thread-safely to asyncio. Cancelling the awaiting task signals the turn and waits for its cooperative cleanup. |
cancel_event | A threading.Event, for synchronous callers |
resume_turn(continuation) | Consumes an existing continuation and its frozen request; adds no user message and reapplies no changed instance settings |
| Pause, steering, continuation admission | The production driver's execution-control service; the Agent API adds no second job registry |
| Tool deadline | Does not cancel the parent conversation |
API reference · Function metadata