An AI agent that can only answer questions using its training data has an important limitation: it cannot reliably work with information that changes after training.
For applications involving technical documentation, product research, news monitoring, or knowledge retrieval, developers often need to connect language models to external information sources.
The application must search the web, retrieve relevant pages, extract useful content, and pass that information back into the reasoning process.
DeepSeek's web search and URL reading capabilities provide additional building blocks for these workflows. Rather than treating web retrieval as a single operation, developers should understand how search, fetching, filtering, and reasoning fit together.
Official references:
2. Understand the Difference Between Search and Fetch
There are two useful approaches to retrieving external information.
The first is model-directed tool execution. The model receives access to web search and URL reading tools. Based on the user's request, it determines whether external information is necessary.
The second is application-controlled retrieval. Your backend explicitly calls a Search API, receives structured results, and decides which pages should be retrieved.
| Capability | Model-directed tools | Application-controlled retrieval |
|---|---|---|
| Search decision | Model | Application |
| Result handling | Model-oriented | Backend-controlled |
| Custom ranking | Limited application control | More flexible |
| Agent integration | Convenient | Requires additional logic |
| RAG integration | Possible | Suitable for custom pipelines |
For a conversational assistant, model-directed tools can reduce application complexity. For a production research pipeline, application-controlled retrieval provides more predictable control over data processing.
3. Configure the DeepSeek API Client
Install the required Python packages:
pip install requests python-dotenv
Create an environment file:
DEEPSEEK_API_KEY=your_api_key
Never place API credentials directly in a public repository.
Configure a reusable client:
import os
import requests
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv("DEEPSEEK_API_KEY")
BASE_URL = "https://api.deepseek.com"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
A reusable configuration makes it easier to switch models or endpoints without modifying every request in the application.
4. Implement Search and Fetch Separately
The Search API and URL fetching functionality should be treated as separate operations.
A conceptual search function:
def search_web(query):
response = requests.post(
SEARCH_ENDPOINT,
headers=headers,
json={
"query": query
},
timeout=30
)
response.raise_for_status()
return response.json()
The SEARCH_ENDPOINT and request parameters must be taken from the specific Search API documentation or provider configuration. Do not assume that every DeepSeek-compatible endpoint exposes the same tool interface.
After receiving results, filter out irrelevant pages and duplicates.
def select_results(results, limit=3):
selected = []
seen_urls = set()
for item in results:
url = item.get("url")
if not url or url in seen_urls:
continue
seen_urls.add(url)
selected.append(item)
if len(selected) >= limit:
break
return selected
The next step is retrieving selected pages through the documented fetching interface.
def fetch_page(url):
response = requests.post(
FETCH_ENDPOINT,
headers=headers,
json={"url": url},
timeout=30
)
response.raise_for_status()
return response.json()
Keep the endpoint and request schema configurable. This makes the implementation easier to adapt when switching providers.
5. Build a Retrieval Workflow
A practical research agent should not pass every retrieved document directly into the model.
A better architecture is:
- Receive the user question.
- Search for relevant pages.
- Remove duplicate results.
- Rank candidate pages.
- Fetch selected content.
- Normalize and truncate the retrieved text.
- Pass the evidence to the model.
- Generate an answer with source attribution.
A simple context builder:
def build_context(documents):
context = []
for document in documents:
context.append({
"source": document.get("url"),
"content": document.get("content", "")
})
return context
The model should receive both the extracted information and its source.
A useful system instruction:
Answer using the retrieved documents.
1. Do not invent facts absent from the sources.
2. Distinguish source statements from your own analysis.
3. Preserve source URLs.
4. Identify conflicting information.
5. State when the retrieved information is insufficient.
Retrieved web pages must be treated as untrusted input. A page may contain instructions that attempt to override the agent's original task. The application should treat retrieved content as data, not executable instructions.
6. Protect the Retrieval Layer
Web retrieval introduces security concerns that ordinary text generation does not.
A production application should consider:
- Blocking private IP ranges and internal network addresses.
- Validating URL schemes.
- Restricting redirects.
- Applying request timeouts.
- Limiting response sizes.
- Preventing server-side request forgery.
- Treating page content as untrusted input.
This is particularly important when users can submit arbitrary URLs.
7. Where a Unified API Gateway Fits
Once an application supports multiple model providers, retrieval is only one part of the infrastructure.
The development team must also maintain model identifiers, API credentials, endpoint configurations, timeout policies, and provider-specific request formats.
A unified gateway can simplify this configuration layer.
For projects that need access to multiple model families, 4SAPI provides a unified API access point for supported models. This can be useful when developers want to manage model access through a common endpoint rather than maintaining separate provider configurations for every project.
A gateway does not replace retrieval logic, source validation, or agent security controls. Those responsibilities remain within the application.
8. Production Checklist
Before deploying a research agent, verify:
- Search and fetch requests have separate timeout settings.
- API errors are handled explicitly.
- Retrieved URLs are validated.
- Duplicate pages are removed.
- Source metadata is preserved.
- Retrieved content is treated as untrusted input.
- Token usage is monitored.
- The application has a maximum retrieval depth.
- Safety checks are enabled where required.
Conclusion
The important architectural decision is not simply whether an AI model can search the web. It is how much control the application needs over retrieval, filtering, verification, and execution.
For simple conversational applications, model-directed tools can be convenient. For complex agents and RAG systems, separating search, fetch, and reasoning often provides a clearer engineering boundary.
Top comments (0)