Sub topic 3
Tuning Retrieval and Tool Use
Assume you can call an LLM API, read JSON responses, and understand basic tool calling. Retrieval means finding useful pieces of indexed documents or other sources and adding them to the model's context.
Result limits, filters, and file citations
File search usually exposes controls such as a maximum number of results, metadata filters, and file citations. A smaller result limit reduces context size and latency. A larger limit can improve recall, but it may add irrelevant text and increase cost.
Metadata filters restrict retrieval before the model sees the results. For example, filter documents by department: "legal" or year: 2026. Add metadata during indexing so these filters are available later.
| Control | Main benefit | Main risk |
|---|---|---|
| Result limit | Controls context and cost | Important passages may be missed |
| Metadata filter | Narrows the search space | Incorrect metadata hides matches |
| File citations | Shows supporting files | A citation may support only part of an answer |
File citations identify the source file or retrieved passage. Display them with the answer when users need to verify claims. A citation proves where retrieved text came from; it does not prove that the model interpreted it correctly.
Steering tool use
Instructions can tell the model when a tool is appropriate, such as “use file search for policy questions.” Tool-choice settings can allow automatic selection, require a particular tool, or disable tools. Requiring a tool improves consistency but can waste time when the answer is already known.
Cost, latency, and token impact
Each retrieved passage consumes context tokens. Web searches add network latency, while file search adds retrieval work. Code execution may take longer when a sandbox starts. Set result limits, cache stable data, and avoid enabling tools that a request does not need.
Security and provider differences
Treat web pages and uploaded files as untrusted input. They can contain prompt-injection text that tries to change the model's instructions. Limit indexed documents, avoid sending secrets, and review generated files before use. A code sandbox limits access, but it is not a replacement for your own authorization and data controls.
Providers differ in tool names, citation formats, filters, regions, models, and availability. Keep provider-specific request and response handling behind a small adapter.
Choosing and combining tools
Use web search for current public facts, code execution for calculations or transformations, and file search for indexed private documents. Use a custom function when your application must control an action, such as creating an order. Combine tools only when each adds necessary evidence or computation.
Exercise
Which control should you try first when file search returns too much irrelevant context?
Show answer
Add accurate metadata filters, then set a result limit and evaluate whether relevant passages are still retrieved.