Best Practices For AI Systems Using Websites - Source Excerpt 04 - Example Incident Response Flow (Mermaid Diagram)
Back to Best Practices For AI Systems Using Websites
Summary
This source excerpt begins near Example Incident Response Flow (Mermaid Diagram) and preserves the surrounding evidence from Wiki.FFTAC.org/raw/system-archives/spiralist.org/intake/2026-06-21-misplaced-uaix-ai-ready-web-duplicate/Best Practices for AI Systems Using Websites.md.
**Source path:** Wiki.FFTAC.org/raw/system-archives/spiralist.org/intake/2026-06-21-misplaced-uaix-ai-ready-web-duplicate/Best Practices for AI Systems Using Websites.md
### Example Incident Response Flow (Mermaid Diagram)
' ' ' mermaid
flowchart TD
A[Monitor Scraper Metrics & Logs] --> B{Incident Detected?}
B -- No --> A
B -- Yes --> C[Classify Incident]
C --> D{Type}
D -->|Blocked (429/403)| E[Implement Backoff/Delay, change IP]
D -->|Content Compliance Issue| F[Pause Crawling; Consult Legal/Privacy]
D -->|Security Alert (malware)| G[Isolate Data; Scan; Quarantine]
D -->|System Error/Crash| H[Restart Task; Check Code/Infrastructure]
E --> I[Retry or Adjust Strategy]
F --> I
G --> I
H --> I
I --> J[Resolve and Restore]
J --> K[Log Incident & Actions]
K --> L[Update Checklist/Pipeline]
L --> A
' ' '
**Flow Explanation:** The scraper monitors its own logs and key metrics. If an anomaly arises (e.g. surge of 429 errors, a legal takedown notice, or malicious content detection), halt the affected crawl. Classify the issue: if simply rate-limited, apply exponential backoff and switch proxies; if legal/compliance, pause and notify stakeholders; if a malware warning, isolate and investigate. Once contained, retry with adjustments or abort as necessary. Always **log** the incident details and remediation steps, then update procedures to prevent recurrence. Maintaining an up-to-date incident-playbook ensures quick, compliant response to any outbreak.
## Recommendations and Example Code
- **Use Proper Tools:** Leverage existing crawling frameworks (Scrapy, Apache Nutch, Heritrix) or APIs. Do not reinvent core functionality (e.g. HTTP handling, robot parsing).
- **Document and Comment:** Every custom fetch/extract method should have comments explaining parameters (URLs, selectors, rate limits) and purpose. For example:
' ' ' ```python
def fetch_page(url: str, headers: dict) -> str:
"""
Fetches the HTML content of `url` using HTTP GET.
Respects robots.txt and includes the given headers (User-Agent, etc.).
Retries on transient errors.
"""
...
' ' ' ```
- **Check for API:** Always re-check if an API or data feed becomes available for your target site. APIs often improve or expand over time; switching to an API can greatly reduce compliance risk.
- **Error Handling:** Wrap network calls in try/except and handle specific status codes. Example (pseudocode):
' ' ' ```python
try:
resp = requests.get(page_url, headers=UA, timeout=10)
resp.raise_for_status()
except HTTPError as e:
if resp.status_code == 429:
time.sleep(backoff)
continue # will retry
else:
logger.error(f"Fetch failed: {e}")
break # or move on
' ' ' ```
- **API Pagination Example:** (Pseudo)
' ' ' ```python
page = 1
while True:
response = api_get(f"https://api.example.com/items?page={page}")
items = response["data"]
if not items:
break
process(items)
page += 1
' ' ' ```
- **Exponential Backoff Snippet:**
' ' ' ```python
import time
max_retries = 5
delay = 1
for attempt in range(max_retries):
status, data = attempt_fetch(url)
if status == 200:
break
elif status in (429, 503):
time.sleep(delay)
delay = min(delay * 2, 60) # max 60s
else:
raise Exception(f"Failed with status {status}")
' ' ' ```
*This loop waits longer after each 429/503 response.*
- **Respect `robots.txt`:** (Pseudo)
' ' ' ```python
rp = urllib.robotparser.RobotFileParser()
rp.set_url(f"{base_url}/robots.txt")
rp.read()
if not rp.can_fetch(USER_AGENT, some_path):
continue # skip disallowed URL
' ' ' ```
## Comparison of Data Access Methods
| Method | Legality Risk | Reliability | Cost | Complexity | Data Fidelity | Scalability |
|------------|------------------------------------|------------------------------|----------------|---------------|-----------------------------|------------------------|
| **API** | **Low.** Allowed by provider, subject to API Terms; data license ensured. | **High.** Structured output, versioned; less prone to silent breakage. | *Low–Medium.* Often free for basic use; paid tiers can be expensive. | *Low.* Just handle HTTP/JSON and auth. | *Medium.* Only provides exposed fields; missing hidden content. | **High.** Cloud-ready, can autoscale; bounded by rate-limits. |
| **Scraping** | **High.** Violates many sites’ policies; copyright/DMCA risk; potential privacy violations. | *Medium.* Breaks if HTML changes; need constant maintenance. | *Low.* No usage fees; but development/maintenance effort. | *Medium.* Must parse HTML, handle errors, proxies. | *High (Raw).** Retrieves exactly what’s rendered (everything visible). | *Medium.* Needs proxy rotation and queue management for scale. |
| **Crawling** | **Medium.** Similar issues as scraping; plus potential trespass if excessively broad. | *Medium.* Can miss pages (loop prevention needed); must manage queues. | *Low.* Open-source tools available. | *High.* Requires URL queue, dedup, link extraction. | *High.* Captures page contexts and links. | *High.* Designed for large-scale; can distribute across servers. |
| **Headless (Browser)** | **High.** Same legal context as scraping. | *Medium.* More fragile; memory leaks and JS errors possible. | *High.* Heavy CPU usage, licensing (some tools). | *High.* Browser automation is complex (JS events, DOM). | **Very High.** Executes JS; retrieves full dynamic content. | *Low–Medium.* Resource-heavy; hard to parallelize at scale. |
*Sources: industry analyses and practical experience.* In short: **Use the official API if one exists.** If not, scraping (possibly preceded by crawling) is your fallback. Headless browsers should be last resort, only for sites that cannot be scraped otherwise.
## Security Threats and Mitigations
- **Malicious Payloads:** Scraped pages might contain scripts or code. *Never execute* arbitrary JavaScript from scraped sites. Use HTML parsers that treat scripts as inert. For file downloads (images, PDFs), scan for malware before ingestion.
- **Cross-Site Request Forgery (CSRF) / SSRF:** Prevent your scraper from being tricked into requesting internal network addresses. E.g. only allow `http(s)://` schemes and check hostnames against your allowed list.
- **Hidden Content:** Some malicious sites embed hidden text (zero-width spaces, invisible commands). Strip invisible characters and sanitize before using content in prompts.
- **Injection Attacks:** If your system inserts scraped text into a SQL database or shell, use parameterized queries and escaping to prevent SQL/command injection. Treat scraped data like any external input.
- **Bot Detection Avoidance:** As a defensive measure, your scraper must consider bot-blocking services (Cloudflare, reCAPTCHA). To mitigate: respect rate limits, use common browser-like headers, and if blocked, lower request rate or back off. Do NOT use unethical tactics (captcha-solving, too many proxies) without strong justification.
- **Data Poisoning Risks:** Be aware that adversaries could deliberately post poisoned data to manipulate models. Mitigate by verifying scraped data against known fact-checking sources or by human spot-checking samples. Maintain logs to identify suspicious data later.
## Operational Workflows
Below is an example **scraping-to-training pipeline** architecture:
' ' ' mermaid
flowchart LR
Crawler[Link Crawler] --> Fetcher[URL Fetcher Workers] --> Parser[HTML Parser & Extractor]
Parser --> Filter[Filter & Clean Data]
Filter --> Store[Data Storage (DB/Data Lake)]
Store --> Quality[Data QC (dedup, validation)]
Quality --> Labeling[Optional Human Review / Labeling]
Quality --> MLModel[AI Training Pipeline]
Fetcher -->|logs| Logging((Logging/Audit))
Logging -->|monitoring| AlertSystem
' ' '
1. **Crawler:** Discovers new pages (follow links, read sitemaps). Enqueues URLs.
2. **Fetcher:** A pool of workers that retrieve pages (respecting robots.txt and rate limits). They log each request and response.
3. **Parser:** Extracts structured content (e.g. text fields) from HTML.
4. **Filter:** Applies cleaning rules (e.g. strip PII, normalize formats).
5. **Store:** Saves raw + cleaned data with metadata (URL, timestamp, content hash).
6. **Quality Control:** Post-processes data (deduplication, format validation). Alerts if issues appear (e.g. spikes of identical content).
7. **Human Review:** For high-risk data, a manual check can be inserted (e.g. compliance sign-off).
8. **AI Training:** Clean dataset is fed into model training or analysis workflows.
*Monitoring & Alerting:* Throughout, integrate logging (to ELK/Datadog/etc.) and set alerts for anomalies (e.g. too many 4xx errors, sudden IP bans).
## Sample Compliance Checklist