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.
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.
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).
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.
- 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.
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.
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
tools=[{"type": "web_search"}]enables the hosted web-search tool — no API key for a search provider, no scraping, no loop.- 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.
- The final grounded answer is on
resp.output_textas usual;resp.outputalso 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.
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
- You index documents into a vector store (OpenAI hosts the embeddings + index); its id is
vs_.... {"type": "file_search", "vector_store_ids": [...]}enables retrieval over those stores — the model searches them when the question calls for it.- 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.
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.
| Approach | Who runs it | Use when |
|---|---|---|
| Hosted tool | OpenAI | the capability (web/file search, code) ships built-in |
| Function tool | your code | the logic is bespoke to your app |
| MCP server | a server you run/adopt | a 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
- Beginner: run Lab OP5.1 and ask a current-events question.
- Easy: inspect
resp.outputto see the web-search call items alongside the text. - Core: create a vector store, add a doc, and answer from it with Lab OP5.2.
- Stretch: enable two hosted tools at once and ask a question that needs both.
- Hard: compare hosted file_search answers against your hand-built Ch 3 RAG on the same questions.
- 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
toolsentry. - Choose between hosted, function, and MCP for a capability.
- Reason about hosted-tool cost, latency, and injection risk.
Knowledge check check yourself
What distinguishes a hosted (built-in) tool from a function tool, and how do you enable web search?
Show answer
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.How does hosted file_search relate to the RAG you built by hand, and when would you still build your own?
Show answer
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.