Skip to main contentSkip to footer
ExamplescriptintermediateRunnableresearch-brief

Search Agent

Runnable example (intermediate) for script using docling, openai.

Key Facts

Level
intermediate
Runtime
Python • OpenAI API
Pattern
Context-backed research with explicit evidence
Interaction
Live sandbox • Script
Updated
14 March 2026

Navigate this example

High-level flow

How this example moves from input to execution and reviewable output
Search Agent -> Retrieve relevant context -> Run the agent task -> User request -> System execution -> Reviewable output

Trigger

Search Agent

Runtime

Retrieve relevant context

Outcome

Run the agent task

Why this page exists

This example is shown as both real source code and a product-facing interaction pattern so learners can connect implementation, UX, and doctrine without leaving the library.

Visual flowReal sourceSandbox or walkthroughMCP access
How should this example be used in the platform?

Use the sandbox to understand the experience pattern first, then inspect the source to see how the product boundary, model boundary, and doctrine boundary are actually implemented.

UX pattern: Context-backed research with explicit evidence
Apply progressive disclosure to system agency
Expose meaningful operational state, not internal complexity
Establish trust through inspectability
Source references
Library entry
context-web-4-search-agent
Source path
content/example-library/sources/context/web/4-search-agent.py
Libraries
docling, openai, pydantic, python-dotenv
Runtime requirements
OPENAI_API_KEY
Related principles
Apply progressive disclosure to system agency, Expose meaningful operational state, not internal complexity, Establish trust through inspectability, Represent delegated work as a system, not merely as a conversation

4-search-agent.py

python
import json

from openai import OpenAI

from tools import (
    AgentAnswer,
    get_handbook_tool,
    get_web_page,
    get_web_page_tool,
    get_web_search_tool,
    search_handbook,
)

MODEL = "gpt-4.1"
client = OpenAI()

SYSTEM_PROMPT = """You are a research assistant for Dutch government organizations. You can help answer questions by:
1. Searching the AI implementation handbook (for policy questions)
2. Fetching specific web pages (when given a URL)
3. Performing wider web searches (for general information)

Use the most appropriate tool(s) based on the question. Provide clear answers with citations."""

tools = [
    get_handbook_tool(),
    get_web_page_tool(),
    get_web_search_tool(
        allowed_domains=["rijksoverheid.nl", "tweedekamer.nl", "cbs.nl"]
    ),
]


def call_function(name: str, args: dict) -> str:
    """Execute a tool function."""
    if name == "search_handbook":
        return search_handbook(**args)
    elif name == "get_web_page":
        return get_web_page(**args)
    raise ValueError(f"Unknown function: {name}")


def ask_agent(query: str) -> AgentAnswer:
    """Ask the agent a question. It will decide which tools to use."""
    input_messages = [{"role": "user", "content": query}]

    response = client.responses.create(
        model=MODEL,
        input=input_messages,
        tools=tools,
        instructions=SYSTEM_PROMPT,
    )

    tool_calls_made = []
    for output_item in response.output:
        input_messages.append(output_item)

        if output_item.type == "function_call":
            name = output_item.name
            args = json.loads(output_item.arguments)
            print(f"Tool called: {name}")
            if name == "get_web_page":
                print(f"  URL: {args.get('url', 'N/A')}")
            elif name == "search_handbook":
                print(f"  Query: {args.get('query', 'N/A')}")

            result = call_function(name, args)
            tool_calls_made.append(name)

            if name == "get_web_page":
                print(f"  Retrieved {len(result)} characters")
            elif name == "search_handbook":
                print(f"  Handbook retrieved ({len(result)} chars)")

            input_messages.append(
                {
                    "type": "function_call_output",
                    "call_id": output_item.call_id,
                    "output": result,
                }
            )

        # Check for web_search tool usage (built-in tool)
        if output_item.type == "web_search_call":
            print("Tool called: web_search")
            tool_calls_made.append("web_search")

    if not tool_calls_made:
        print("No tool call needed - responding directly")
        final_response = client.responses.parse(
            model=MODEL,
            input=input_messages,
            instructions=SYSTEM_PROMPT,
            text_format=AgentAnswer,
        )
    else:
        print(f"Tools used: {', '.join(tool_calls_made)}")
        while True:
            final_response = client.responses.parse(
                model=MODEL,
                input=input_messages,
                tools=tools,
                instructions=f"{SYSTEM_PROMPT} Use the retrieved information to provide a comprehensive answer. Include 2-4 key citations with text excerpts and sources (URLs or section numbers).",
                text_format=AgentAnswer,
            )

            # Check if there are more tool calls needed
            more_tool_calls = False
            for output_item in final_response.output:
                if output_item.type == "function_call":
                    more_tool_calls = True
                    name = output_item.name
                    args = json.loads(output_item.arguments)
                    print(f"Additional tool called: {name}")

                    result = call_function(name, args)
                    input_messages.append(output_item)
                    input_messages.append(
                        {
                            "type": "function_call_output",
                            "call_id": output_item.call_id,
                            "output": result,
                        }
                    )

            if not more_tool_calls:
                break

    # Extract parsed content
    for output_item in reversed(final_response.output):
        if hasattr(output_item, "content") and output_item.content:
            for content_item in reversed(output_item.content):
                if hasattr(content_item, "parsed") and content_item.parsed:
                    return content_item.parsed

    raise ValueError("Could not find parsed response in output")


# Example usage
if __name__ == "__main__":
    examples = [
        {
            "name": "1. No tool call (direct response)",
            "query": "What can you do?",
        },
        {
            "name": "2. Handbook search only",
            "query": "What are the requirements for registering an AI system in the Algorithm Register?",
        },
        {
            "name": "3. Get specific web page",
            "query": "Can you fetch and summarize the content from https://www.europarl.europa.eu/topics/en/article/20230601STO93804/eu-ai-act-first-regulation-on-artificial-intelligence?",
        },
        {
            "name": "4. Web search only",
            "query": "Use web search to find current policies about AI implementation in Dutch government services on official government websites.",
        },
        {
            "name": "5. Multiple tool calls (handbook + web page)",
            "query": "First, search the handbook for IAMA requirements. Then fetch the EU AI Act page at https://www.europarl.europa.eu/topics/en/article/20230601STO93804/eu-ai-act-first-regulation-on-artificial-intelligence and compare the requirements.",
        },
    ]

    for example in examples:
        print(f"\n{'=' * 70}")
        print(f"{example['name']}")
        print(f"Query: {example['query']}")
        print(f"{'=' * 70}\n")
        result = ask_agent(example["query"])
        print(f"\nAnswer: {result.answer}\n")
        if result.citations:
            print("Citations:")
            for citation in result.citations:
                source = citation.url or f"Section {citation.section}"
                print(f"  {source}: {citation.text[:100]}...")
        print()
What should the learner inspect in the code?

Look for the exact place where system scope is bounded: schema definitions, prompt framing, runtime configuration, and the call site that turns user intent into a concrete model or workflow action.

Look for output contracts and validation
Look for the exact execution call
Look for what the product could expose to the user
How does the sandbox relate to the source?

The sandbox should make the UX legible: what the user sees, what the system is deciding, and how the result becomes reviewable. The source then shows how that behavior is actually implemented.

Enter a question or load a sample query.
Run the search or retrieval step.
Review the final brief with sources and retrieved context.
SandboxContext-backed research with explicit evidence
Research brief lab

This sandbox shows how a search or retrieval request should expose query planning, retrieved context, and the final answer.

UX explanation

The user should not only see a final answer. The product should reveal what was searched, what context shaped the response, and where the system boundary stops.

AI design explanation

These examples combine retrieval, web search, or file context with a synthesis step. The best surface exposes search plan, useful evidence, and a reviewable output.

Interaction walkthrough

  1. 1Enter a question or load a sample query.
  2. 2Run the search or retrieval step.
  3. 3Review the final brief with sources and retrieved context.

Research question

Search planRetrieved context

Plan

The search plan appears here.

Final brief

The final brief appears here alongside the context used.

Used in courses and paths

This example currently stands on its own in the library, but it still connects to the principle system and the broader example family.

Related principles

Runtime architecture

Use this example in your agents

This example is also available through the blueprint’s agent-ready layer. Use the For agents page for the public MCP, deterministic exports, and Claude/Cursor setup.

Define triggers, context, and boundaries before increasing autonomy
Make control, observability, and recovery explicit in the runtime
Choose the right operational patterns before delegating to workflows