# Embedding in your own stack

A library call starts neither the Web UI nor the terminal UI, and does not replace the installed product.

## Host Runtime

```
from openprogram import Agent, Runtime


def model_call(content, model="host-model", response_format=None):
    # Replace this offline response with the host's model client.
    return "Host model response"

runtime = Runtime(call=model_call, model="host-model")

class ReviewAgent(Agent):
    tools = []

    def classify(self, review: str) -> str:
        """Classify review sentiment."""
        return self(f"Classify review sentiment:\n{review}")

    def summarize(self, review: str) -> str:
        """Summarize a review."""
        sentiment = self.classify(review)
        return self(f"Summarize this {sentiment} review:\n{review}")

reviewer = ReviewAgent(runtime=runtime)
try:
    summary = reviewer.summarize("The service was fast.")
finally:
    runtime.close()
```

The callback gets content blocks, a model name and an optional response format; this one returns a fixed response and needs no network. Constructing a Runtime makes no model request. The host closes the Runtime it supplies. Agent methods inherit it; Context and function scopes are automatic.

## Explicit session

```
from openprogram import Agent, Context
from openprogram.state.store import SessionStore, SessionNodeWriter

store = SessionStore(root_path="/var/lib/myapp/sessions")
store.create_session("research", agent_id="main")
writer = SessionNodeWriter(store, "research")
researcher = Agent(context=Context(store=writer), tools=[])
try:
    first = researcher("List the project requirements.")
    second = researcher("Review the requirements from the previous call.")
finally:
    store.close()
```

Default Agent calls are independent; `Context(store=writer)` continues this session. `head_id` can select an existing branch tip. The host owns its store and Runtime.

## Inspect calls

```
from openprogram.state.store import SessionNodeWriter

for node in SessionNodeWriter(store, "review-42").load():
    print(node.name, node.caller, node.output)
```

To expose a method as a model tool, set `tool=True` in `method_options`, which supplies `.spec` and `.execute`. Recording alone grants no tool access.

## Host hooks

| Hook | In |
| --- | --- |
| `set_cancellation_check`, `CancelledError`, `check_cancelled` | `openprogram.host.cancellation` |
| `set_session_id_provider` (question routing) | `openprogram.runtime.agent.call_state` |

Import checks: [`test_standalone_embed.py`](https://github.com/Fzkuji/OpenProgram/blob/main/tests/component/runtime/test_standalone_embed.py).

[Runtime](https://fzkuji.github.io/OpenProgram/docs/reference/api/runtime.html) · [Agent and Context API](https://fzkuji.github.io/OpenProgram/docs/reference/api/agent.html) · [Agent calls and methods](https://fzkuji.github.io/OpenProgram/docs/capabilities/agentic-programming/writing-functions/agent.html)
