TL;DR
We built a TypeScript app that:
- Converts API specs into machine-first storefront pages
- Routes tasks dynamically across discovery, enrichment, and inference providers
- Executes paid API calls via AgentCash
- Sends outreach and summary emails autonomously from the agent
- Produces run artifacts with traces (provider, latency, cost, success)
This post explains architecture, design choices, and practical implementation details.
Problem Statement
Most “AI automation” demos stop at content generation. Real agentic commerce needs:
- Transactional execution rails (pay per request)
- Real-time data for targeting and personalization
- Multi-provider routing to optimize quality/cost/speed
- Proof-of-delivery (actual sent artifacts + logs)
We designed this app around those constraints.
System Overview
Provider Strategy
AgentCash (execution + payments)
AgentCash is the payment and execution spine:
- endpoint checks
- paid fetch calls
- email sends via
stableemail
Tavily + Bright Data (research/enrichment)
- Tavily for broad, fast web signal collection
- Bright Data for deeper MCP-enabled data workflows and web tooling
OpenAI + Featherless (inference layer)
- OpenAI for high-quality strategic copy
- Featherless for cost-effective, OpenAI-compatible bulk generation
This split lets us optimize per-step rather than locking everything to one vendor.
Code Walkthrough
1) Typed env schema
Using Zod, we enforce env correctness at startup.
const envSchema = z.object({
DRY_RUN: z.string().optional().transform((v) => (v ?? "true").toLowerCase() === "true"),
BRIGHT_DATA_API_TOKEN: z.string().optional(),
FEATHERLESS_BASE_URL: z.string().default("https://api.featherless.ai/v1"),
FEATHERLESS_MODEL: z.string().default("meta-llama/Meta-Llama-3.1-8B-Instruct")
});
2) Capability router
A policy-driven router selects providers based on task type and strategy.
forResearchTask(): RouteDecision[] {
if (this.policy === "speed") {
return [
{ provider: "tavily", reason: "Fast web research baseline" },
{ provider: "agentcash", reason: "Paid enrichment calls" }
];
}
// quality / cost variants...
}
3) Featherless with OpenAI-compatible API
await fetch(`${env.FEATHERLESS_BASE_URL}/chat/completions`, {
method: "POST",
headers: {
Authorization: `Bearer ${env.FEATHERLESS_API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: env.FEATHERLESS_MODEL,
messages: [
{ role: "system", content: "Write concise outbound personalization lines." },
{ role: "user", content: prompt }
]
})
});
4) Email send contract gotcha
We initially used cc, but stableemail.dev/api/send validates to as array and does not accept cc.
Correct pattern:
await this.agentcash.fetch("https://stableemail.dev/api/send", {
to: ["primary@example.com", "observer@example.com"],
subject,
text
});
5) Run artifact generation
await writeFile(`output/${runId}.md`, reportMarkdown, "utf-8");
await writeFile(`output/${runId}.brightdata.mcp.json`, JSON.stringify(mcpConfig, null, 2), "utf-8");
Data and Control Flow
Running It
npm install
cp .env.example .env
npm run dev
npm run start
Verification Checklist
- Startup logs include
dryRun=falsefor real runs - Output report has non-zero latencies for paid sends
- AgentCash balance drops after real execution
- Inbox receives summary mail from
relay@stableemail.dev
What We Learned
- Schema-first integration saves time. Use
agentcash checkbefore coding payloads. - “Provider abstraction” is useful only if it maps to real contract differences.
- Run artifacts are essential for trust and debugging.
- The right metric is not just “emails sent,” but conversion and repeat paid calls.
Future Improvements
- Add persistent DB for lead state and campaign progression
- Add idempotency keys for send operations
- Add per-provider circuit breaker and retries
- Add UI dashboard with run drill-down
- Add evaluator loop for subject line and CTA optimization
This project demonstrates a practical path from “AI workflow” to “agentic commerce engine.” It is intentionally modular so teams can swap providers while preserving the core orchestration model.
Github Repo: https://github.com/harishkotra/AgentCash-Commerce-Router/



Top comments (0)