Pricing Tools

Calculate dynamic prices, apply discounts, and build bundles.

Overview

Pricing tools implement dynamic pricing logic that considers inventory, competition, customer loyalty, and policy constraints. They ensure your agents never price below floor while maximizing margin.

calculateDynamicPrice

Calculate optimal pricing with all context factors:

import { calculateDynamicPrice, getProduct } from '@hyperfold/actions-sdk';
const product = await getProduct('prod_aero_x2');
// Calculate dynamic price with full context
const pricing = await calculateDynamicPrice(product, {
  // Buyer context
  buyerTier: 'gold',
  buyerHistory: {
    totalOrders: 12,
    totalSpent: 2450,
    avgOrderValue: 204,
  },
  // Cart context
  cartValue: 180,
  cartItems: 1,
  // Inventory context
  inventoryLevel: 847,
  inventoryStatus: 'normal',
  // Competition
  competitorPrice: 165,
  // Time factors
  dayOfWeek: 'tuesday',
  timeOfDay: 'afternoon',
});
// Returns:
{
  floor: 92.00,        // Absolute minimum (cost + min_margin)
  target: 162.00,      // Ideal price for good margin
  suggested: 155.00,    // What to counter-offer
  list: 180.00,        // Original list price
  adjustments: [
    { type: 'loyalty', description: 'Gold tier discount', amount: -18.00 },
    { type: 'competitor', description: 'Beat Amazon by $10', amount: -7.00 },
  ],
  totalDiscount: 25.00,
  discountPercent: 13.9,
  margin: 46.4,
  marginDollars: 63.00,
  explanation: "As a Gold member, you qualify for 10% off. " +
               "This price also beats Amazon by $10.",
}

getCompetitorPrice

Fetch competitor pricing for competitive positioning:

import { getCompetitorPrice } from '@hyperfold/actions-sdk';
// Get competitor price for a product
const competitorData = await getCompetitorPrice('prod_aero_x2');
// Returns:
{
  product_id: 'prod_aero_x2',
  competitors: [
    {
      name: 'Amazon',
      price: 165.00,
      url: 'https://amazon.com/...',
      last_updated: '2025-12-19T14:00:00Z',
      in_stock: true,
    },
    {
      name: 'Walmart',
      price: 172.00,
      url: 'https://walmart.com/...',
      last_updated: '2025-12-19T13:45:00Z',
      in_stock: true,
    },
  ],
  lowest_price: 165.00,
  lowest_competitor: 'Amazon',
  avg_price: 172.33,
}
// Get specific competitor
const amazonPrice = await getCompetitorPrice('prod_aero_x2', {
  competitor: 'amazon',
});
// Refresh competitor prices (cache bypass)
const freshPrices = await getCompetitorPrice('prod_aero_x2', {
  refresh: true,
});

applyDiscount

Apply discounts with policy validation:

import { applyDiscount } from '@hyperfold/actions-sdk';
// Apply percentage discount
const discounted = await applyDiscount({
  product_id: 'prod_aero_x2',
  original_price: 180.00,
  discount_type: 'percentage',
  discount_value: 10,
  reason: 'Gold member discount',
});
// Returns:
{
  original_price: 180.00,
  discount_amount: 18.00,
  final_price: 162.00,
  discount_percent: 10,
  within_policy: true,
  above_floor: true,
}
// Apply fixed amount discount
const fixedDiscount = await applyDiscount({
  product_id: 'prod_aero_x2',
  original_price: 180.00,
  discount_type: 'fixed',
  discount_value: 25,
  reason: 'Price match request',
});
// Apply with validation (50% off - may be capped)
const validated = await applyDiscount({
  product_id: 'prod_aero_x2',
  original_price: 180.00,
  discount_type: 'percentage',
  discount_value: 50,
  reason: 'Customer request',
});
// Returns with warning:
{
  original_price: 180.00,
  discount_amount: 88.00,  // Capped at floor
  final_price: 92.00,      // Floor price
  discount_percent: 48.9,
  within_policy: false,    // Exceeds max 30%
  above_floor: true,
  warning: 'Discount capped at floor price ($92.00)',
  requires_approval: true,
}

calculateBundlePrice

Calculate pricing for product bundles:

import { calculateBundlePrice } from '@hyperfold/actions-sdk';
// Calculate bundle price for multiple products
const bundle = await calculateBundlePrice({
  products: [
    { product_id: 'prod_aero_x2', quantity: 1 },
    { product_id: 'prod_socks_wp', quantity: 2 },
    { product_id: 'prod_insoles_pro', quantity: 1 },
  ],
  buyer_tier: 'gold',
});
// Returns:
{
  items: [
    {
      product_id: 'prod_aero_x2',
      name: 'AeroRun X2 Marathon Shoe',
      unit_price: 180.00,
      quantity: 1,
      subtotal: 180.00,
    },
    {
      product_id: 'prod_socks_wp',
      name: 'Waterproof Running Socks',
      unit_price: 18.00,
      quantity: 2,
      subtotal: 36.00,
    },
  ],
  pricing: {
    subtotal: 251.00,
    bundle_discount: 25.10,    // 10% bundle discount
    loyalty_discount: 22.59,   // Gold tier 10%
    total: 203.31,
    you_save: 47.69,
    savings_percent: 19.0,
  },
  margins: {
    combined_margin: 42.5,
    above_floor: true,
  },
}
// Suggest optimal bundle
const suggestions = await calculateBundlePrice({
  base_product: 'prod_aero_x2',
  suggest_additions: true,
  max_suggestions: 3,
  target_bundle_value: 250,
});

Usage in Agent

Complete example using pricing tools:

import {
  HyperfoldAgent,
  OnACPEvent,
  calculateDynamicPrice,
  getCompetitorPrice,
  applyDiscount,
  getProduct,
} from '@hyperfold/actions-sdk';
@HyperfoldAgent({ name: 'pricing-agent', type: 'negotiator' })
export class PricingAgent {
  @OnACPEvent('quote')
  async handleQuote(productId: string, offer: number, context: BuyerContext) {
    const product = await getProduct(productId);
    const competitors = await getCompetitorPrice(productId);
    const pricing = await calculateDynamicPrice(product, {
      buyerTier: context.loyalty_tier,
      buyerHistory: context.purchase_history,
      cartValue: context.cart_value,
      competitorPrice: competitors.lowest_price,
    });
    if (offer === null) {
      return {
        status: 'quote',
        list_price: pricing.list,
        offered_price: pricing.suggested,
        reasoning: pricing.explanation,
      };
    }
    if (offer >= pricing.target) {
      return { status: 'accept', price: offer };
    }
    if (offer >= pricing.floor) {
      const counter = Math.max(offer + 10, pricing.suggested);
      return {
        status: 'counter_offer',
        counter_price: counter,
        reasoning: `I can do $${counter} - that's ${pricing.discountPercent.toFixed(0)}% off and beats ${competitors.lowest_competitor}.`,
      };
    }
    return {
      status: 'reject',
      reason: 'Below minimum price',
      hint: `Our best price is around $${Math.ceil(pricing.floor / 5) * 5}`,
    };
  }
}