Built-in tools: web search and code

OpenAI API · Practical · 11 min

Hosted tools add live information and real computation without you writing or running any tool code.

Function tools run in your code. Built-in tools run on OpenAI's side: you list them in tools, the model uses them during the same request, and the results are already folded into the answer.

Web search ({"type": "web_search"}) lets the model look up current information. The answer's text carries url_citation annotations, notes attached to the text that give the URL and title of each source. If you show web-based answers to users, show these citations as visible, clickable links. Options include filters with allowed_domains (only search these sites) and a user_location for local results.

Code interpreter ({"type": "code_interpreter", "container": {"type": "auto"}}) lets the model write and run Python in a container, a sealed-off temporary computer. Use it for maths, data analysis and charts. Containers expire after about 20 minutes without use and their files are then lost, so download anything you need while they are active. Files the model creates are cited as container_file_citation annotations.

Other built-in tools include file search (next lesson), image generation, remote MCP servers and computer use.

Built-in tools cost extra on top of tokens; check the pricing page. And treat anything fetched from the web as untrusted: a page can contain text that tries to give the model instructions.

Example

from openai import OpenAI

client = OpenAI()
r = client.responses.create(
    model="gpt-6-astra",
    tools=[
        {"type": "web_search",
         "filters": {"allowed_domains": ["python.org", "docs.python.org"]}},
        {"type": "code_interpreter", "container": {"type": "auto"}},
    ],
    input="What is the latest Python release, and how many days ago was it?",
)

print(r.output_text)
for item in r.output:
    if item.type == "message":
        for part in item.content:
            for a in getattr(part, "annotations", []) or []:
                if a.type == "url_citation":
                    print("source:", a.url)

Remember

  • Built-in tools run on OpenAI's servers; just list them in tools
  • web_search returns url_citation annotations to show users
  • code_interpreter runs Python in a short-lived container

Watch out for

  • Relying on files in a code interpreter container after it expired; they cannot be recovered.

Check yourself

  1. Where do web search sources appear in the response?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: developers.openai.com/api/docs/guides/tools-web-search