# Traceable dataset sampler for AI agents (`rare_sunset/traceable-context-sampler`) Actor

Create a deterministic, representative, token-bounded context package from an Apify dataset while preserving row-level provenance and omission accounting.

- **URL**: https://apify.com/rare\_sunset/traceable-context-sampler.md
- **Developed by:** [Abdulrahman Baidaq](https://apify.com/rare_sunset) (community)
- **Categories:** AI, Agents, MCP servers
- **Stats:** 1 total users, 0 monthly users, 0.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

Pay per usage

This Actor is paid per platform usage. The Actor is free to use, and you only pay for the Apify platform usage, which gets cheaper the higher subscription plan you have.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-usage

## What's an Apify Actor?

Actors are web data automations that power AI and operations. They run on the Apify platform to scrape websites, process data, connect APIs, and automate workflows.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.
Actors are written with capital "A".

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.
The best way to integrate Actors is as follows.

- **AI agents and MCP clients** — the [Apify MCP server](https://docs.apify.com/integrations/mcp.md) at `https://mcp.apify.com` (remote, streamable HTTP, OAuth on first use).
- **Agentic workflows and local Actor development** — [Agent Skills](https://apify.com/.well-known/agent-skills/index.json) with the [Apify CLI](https://docs.apify.com/cli/docs.md): `npm install -g apify-cli`, then `apify login`.
- **JavaScript/TypeScript projects** — the official [JS/TS client](https://docs.apify.com/api/client/js/docs.md): `npm install apify-client`.
- **Python projects** — the official [Python client](https://docs.apify.com/api/client/python/docs.md): `pip install apify-client`.
- **Any other language** — the [REST API](https://docs.apify.com/api/v2.md).

For usage examples, see the [API](#api) section below.

For more details, see Apify documentation as [Markdown index](https://docs.apify.com/llms.txt) and [Markdown full-text](https://docs.apify.com/llms-full.txt).

# README

## Traceable dataset sampler for AI agents

This Actor converts an Apify dataset into a deterministic context package under an explicit complete-artifact transport estimate. It preserves configured provenance fields, suppresses duplicate identities, reserves coverage across configured strata, records why each row was selected, and accounts for every omission.

The size rule is `ceil(complete CONTEXT.md UTF-8 bytes / 3.5)`, not an exact count or guaranteed upper bound for a specific model tokenizer. The Actor does not summarize, rewrite, or invent source values.

### Local tests

```powershell
python -m unittest discover -s tests -v
```

### Output

- Dataset row 1: `context_manifest`
- Remaining rows: selected records with `_context` metadata
- Key-value store: `REQUEST`, `MANIFEST`, and `CONTEXT.md`

# Actor input Schema

## `dataset` (type: `string`):

Apify dataset to sample. The Actor requests read access only.

## `max_context_tokens` (type: `integer`):

Upper bound under ceil(complete CONTEXT.md UTF-8 bytes / 3.5); not a model-tokenizer guarantee.

## `group_fields` (type: `array`):

Fields whose distinct values define strata for minimum coverage.

## `provenance_fields` (type: `array`):

Fields that must be present and non-empty for a row to be eligible.

## `identity_fields` (type: `array`):

Fields used to identify duplicate rows; the complete row hash is the fallback.

## `min_per_stratum` (type: `integer`):

Coverage target reserved for each available stratum before budget filling.

## `max_input_rows` (type: `integer`):

Safety limit on source rows read during one run.

## Actor input object example

```json
{
  "max_context_tokens": 6000,
  "group_fields": [],
  "provenance_fields": [
    "sourceUrl"
  ],
  "identity_fields": [],
  "min_per_stratum": 1,
  "max_input_rows": 10000
}
```

# Actor output Schema

## `records` (type: `string`):

No description

## `manifest` (type: `string`):

No description

## `context` (type: `string`):

No description

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {};

// Run the Actor and wait for it to finish
const run = await client.actor("rare_sunset/traceable-context-sampler").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = {}

# Run the Actor and wait for it to finish
run = client.actor("rare_sunset/traceable-context-sampler").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{}' |
apify call rare_sunset/traceable-context-sampler --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,rare_sunset/traceable-context-sampler"
        }
    }
}

```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/Vfv4fiKlFxtfXAoLe/builds/EUgj7gWmiUUlPR0hx/openapi.json
