AI EngineeringZero to ProductionHome·About·What’s new·Contact
OpenAI API in Practice · Part 5

Built-in Hosted Tools

In the API chapter you hand-wired a function tool and ran the call loop yourself. OpenAI also ships hosted tools that run on their side — web search, file search over your documents, a code interpreter — that you switch on with one line and never have to execute. This chapter covers what they are, when to use them, and the trade vs rolling your own.

⏱️ ~1.5 hours🧪 3 labs🎯 Beginner→Tech-lead

Learning objectives

  • Explain what a hosted (built-in) tool is and how it differs from a function tool.
  • Enable web search with one entry in tools.
  • Enable file search over a vector store of your documents.
  • Decide between a hosted tool, a function tool, and an MCP server.
⚙️ To run this for realNeeds an OpenAI API key (OPENAI_API_KEY) + pip install openai. Hosted tools may carry their own per-call pricing on top of tokens — check current pricing.

1 · Hosted tools vs function tools essential

There are two kinds of tool, and the difference is who runs it. A function tool (the kind you built in the API chapter) is yours: the model emits a function_call, your code runs the real function, you feed the result back. A hosted tool is OpenAI's: you declare it in the tools list, and when the model decides to use it, OpenAI executes it server-side and folds the result back into the answer — your code never sees a tool-call loop at all.

The mental model: a function tool is a tool you bring to the job; a hosted tool is a tool already bolted to the workbench. For capabilities OpenAI has built and operates — searching the live web, searching a vector store of your files, running sandboxed Python — reaching for the hosted version means you don't build, host, or secure the plumbing. You just switch it on.

The common mistake is rebuilding something OpenAI already hosts — hand-rolling a web-search tool with a third-party API and a scraping loop when {"type": "web_search"} would have done it in one line. Conversely, don't force a bespoke capability (your pricing engine) into a hosted tool — that's what function tools and MCP are for (§4).

who executes the tool? — you vs OpenAI function tool (yours) model emits function_call → your code runs it → feed back hosted tool (OpenAI's) declare in tools=[…] → runs server-side, auto-folded You run it, or they do. Function tools execute in your code via the function_call loop; hosted tools (web_search, file_search, code_interpreter) run on OpenAI's servers and the result is merged into the response without a loop on your side.
🗺️ How to read this diagram
  • The blue box is the function-tool flow from the API chapter — you own the execution loop.
  • The green box is a hosted tool — you only declare it; OpenAI runs it and returns a finished answer.
  • Pick by ownership: a capability OpenAI hosts → green (zero plumbing); your bespoke logic → blue.

In short: function tool = you execute; hosted tool = OpenAI executes. Switch on the hosted ones you need and skip building them.

The Anthropic parallelThis is the OpenAI counterpart to Anthropic's server-side tools (ap5). Both vendors host some tools so you don't run them; the names and exact catalog differ, but the idea — provider-executed tools you just enable — is the same.

2 · Web search in one line essential

Give the model live web access by adding one entry to tools. When a question needs current information, the model searches, reads, and answers — citing what it found — with no loop on your side.

Lab OP5.1
web_search.pyfrom openai import OpenAI
client = OpenAI()

resp = client.responses.create(
    model="gpt-5.5",
    tools=[{"type": "web_search"}],       # that's the whole setup
    input="What did OpenAI announce most recently?",
)
print(resp.output_text)                   # answer, grounded in live results
▶ How this works
  1. tools=[{"type": "web_search"}] enables the hosted web-search tool — no API key for a search provider, no scraping, no loop.
  2. The model decides whether to search based on the question; if it does, OpenAI runs the search server-side and the model reads the results before answering.
  3. The final grounded answer is on resp.output_text as usual; resp.output also contains the search-call items if you want to inspect what was searched.

Try this: ask something that happened after the model's training cutoff — without the tool it can't know; with it, it searches and answers. That contrast is the whole value of a live tool.

3 · File search over your documents intermediate

This is managed RAG. Instead of building the retrieval pipeline from Chapter 3 by hand — chunk, embed, store, search — you upload documents into a vector store and enable the hosted file_search tool pointed at it. The model retrieves relevant chunks and grounds its answer, with OpenAI running the retrieval.

Lab OP5.2
file_search.pyfrom openai import OpenAI
client = OpenAI()

# assume you've created a vector store and added files to it;
# vs.id identifies it (see the Files API, oap3).
resp = client.responses.create(
    model="gpt-5.5",
    tools=[{
        "type": "file_search",
        "vector_store_ids": ["vs_abc123"],   # your indexed documents
    }],
    input="What is our refund window, per the policy docs?",
)
print(resp.output_text)                       # grounded in your files
▶ How this works
  1. You index documents into a vector store (OpenAI hosts the embeddings + index); its id is vs_....
  2. {"type": "file_search", "vector_store_ids": [...]} enables retrieval over those stores — the model searches them when the question calls for it.
  3. The answer is grounded in your documents without you writing any chunking, embedding, or search code — hosted RAG.

Try this: compare this with the hand-built RAG in Ch 3. Hosted file_search is faster to ship; the hand-built pipeline gives you full control over chunking and re-ranking. Section 4 is exactly that trade.

Other hosted toolsBeyond web and file search, OpenAI hosts a code_interpreter (sandboxed Python for math/data tasks), image generation, and an mcp tool type that connects hosted MCP servers. Each is enabled the same way — one typed entry in tools.

4 · Hosted vs function vs MCP advanced

Three ways to give a model a capability, one decision. Hosted tool: OpenAI built and runs it (web/file search, code interpreter) — least work, least control, use when it fits your need out of the box. Function tool (ox2): your bespoke code, your execution loop — maximum control, use for logic unique to your app. MCP server (ox4): a reusable tool server, portable across clients — use for general capabilities you'll reuse or share.

ApproachWho runs itUse when
Hosted toolOpenAIthe capability (web/file search, code) ships built-in
Function toolyour codethe logic is bespoke to your app
MCP servera server you run/adopta reusable capability, portable across vendors

5 · Tech-lead — cost, latency & trust tech-lead

Hosted tools are convenient but not free of consequence. They can carry per-call pricing on top of tokens, add latency (a web search is a round trip), and introduce external data into the model's context — which is a prompt-injection surface (a malicious page the web tool reads could carry instructions). The lead-level posture: enable a hosted tool when it genuinely beats building your own, budget for its per-call cost, and remember that content a tool pulls in is untrusted input — the same guardrail discipline from the safety chapters applies.

🪜 Practice ladder beginner → industry

  1. Beginner: run Lab OP5.1 and ask a current-events question.
  2. Easy: inspect resp.output to see the web-search call items alongside the text.
  3. Core: create a vector store, add a doc, and answer from it with Lab OP5.2.
  4. Stretch: enable two hosted tools at once and ask a question that needs both.
  5. Hard: compare hosted file_search answers against your hand-built Ch 3 RAG on the same questions.
  6. Industry: write the decision doc for one real capability: hosted vs function vs MCP, with cost/latency/control reasoning.

✓ Checkpoint — you can move on when you can…

  • Explain hosted vs function tools by who executes them.
  • Enable web search and file search with a tools entry.
  • Choose between hosted, function, and MCP for a capability.
  • Reason about hosted-tool cost, latency, and injection risk.

Knowledge check check yourself

✓ Knowledge check

What distinguishes a hosted (built-in) tool from a function tool, and how do you enable web search?

Show answer
A function tool runs in your code — the model emits a function_call, you execute it and feed the result back. A hosted tool runs on OpenAI's servers — you just declare it and OpenAI executes it, folding the result into the response with no loop on your side. Enable web search by adding {"type": "web_search"} to the tools list; the model searches live and answers from the results.
✓ Knowledge check

How does hosted file_search relate to the RAG you built by hand, and when would you still build your own?

Show answer
Hosted file_search is managed RAG: you index documents into a vector store and enable {"type":"file_search","vector_store_ids":[...]}, and OpenAI runs the chunk/embed/retrieve pipeline for you. You'd still build your own (Ch 3) when you need full control over chunking strategy, hybrid search, or re-ranking — hosted is faster to ship, hand-built is more controllable.
© 2026 studybydoing.in · AI Engineering: Zero to Production · All rights reserved. · About · Privacy Policy · Terms · Contact
Educational content, provided as-is and without warranty. Code samples are examples — review, test, and adapt them before using in production. See the Terms of Use & Disclaimer. Use at your own risk.
© studybydoing.in