Skip to content

AI agent examples

The AI examples give an agent a single run_code tool for JavaScript, TypeScript, and TSX. Both examples ask the agent to use TypeScript to fetch and summarize Hacker News data.

Network access is denied by default. These examples explicitly allow only the hacker-news.firebaseio.com host.

Demonstrates

  • BelgieCapability with Pydantic AI.
  • BelgieMiddleware with LangChain.
  • Agent-authored complete belgie.Script modules.
  • Framework-specific installation and result handling.

Pydantic AI

Install and run the example:

cd examples/ai/pydantic-ai
uv sync
export OPENAI_API_KEY=...
uv run main

The agent is configured with BelgieCapability. The complete entrypoint is included from the shipped example:

from pydantic_ai import Agent

from belgie import RuntimeOptions, RuntimePermissions
from belgie.pydantic_ai import BelgieCapability

runtime_options = RuntimeOptions(
    permissions=RuntimePermissions(allow_net=["hacker-news.firebaseio.com"]),
)

agent = Agent(
    "openai:gpt-5",
    instructions=(
        "You can execute JavaScript, TypeScript, or TSX in a Deno sandbox with the run_code tool. "
        "Use it when fetching data or transforming values is easier in JS/TS than in Python."
    ),
    capabilities=[BelgieCapability(runtime_options=runtime_options)],
)


def main() -> None:
    result = agent.run_sync(
        "Use run_code with a TypeScript belgie.Script module that exports an async run function "
        "to fetch the Hacker News top stories API and summarize the top headline.",
    )
    print(result.output)  # noqa: T201


if __name__ == "__main__":
    main()

The full prompt asks the model to export an async run function from a TypeScript module. See examples/ai/pydantic-ai.

LangChain

Install and run the LangChain example:

cd examples/ai/langchain
uv sync
export OPENAI_API_KEY=...
uv run main

The agent uses BelgieMiddleware. The complete entrypoint is included from the shipped example:

from langchain.agents import create_agent

from belgie import RuntimeOptions, RuntimePermissions
from belgie.langchain import BelgieMiddleware

runtime_options = RuntimeOptions(
    permissions=RuntimePermissions(allow_net=["hacker-news.firebaseio.com"]),
)

agent = create_agent(
    model="openai:gpt-5",
    tools=[],
    middleware=[BelgieMiddleware(runtime_options=runtime_options)],
    system_prompt=(
        "You can execute JavaScript, TypeScript, or TSX in a Deno sandbox with the run_code tool. "
        "Use it when fetching data or transforming values is easier in JS/TS than in Python."
    ),
)


def main() -> None:
    result = agent.invoke(
        {
            "messages": [
                (
                    "user",
                    (
                        "Use run_code with a TypeScript belgie.Script module that exports an async run function "
                        "to fetch the Hacker News top stories API and summarize the top headline."
                    ),
                ),
            ],
        },
    )
    print(result["messages"][-1].content)  # noqa: T201


if __name__ == "__main__":
    main()

See examples/ai/langchain.

Render an inline widget

Change the agent request to ask for a TSX module that returns render(...):

import { render } from "npm:@belgie/render";

function Widget() {
  return <main>Rendered by Belgie</main>;
}

export default function run() {
  return render({ widget: <Widget />, plugins: [] });
}

The result is self-contained HTML. It uses the host renderer described in @belgie/render, not the path-based MCP widget build.

See also