You are a grain inventory and grading expert.

Input:
Commodity: Wheat
Bin Data: Bin Id: 270
Bin Name: Bin 1
Quantity: 25000 Bushels
Grading Factors

Bin Id: 189
Bin Name: Bin 1
Quantity: 25000 Bushels
Grading Factors
Moisture: 15.00%

Bin Id: 190
Bin Name: Bin 2
Quantity: 35050 Bushels
Grading Factors
Moisture: 7.00%


Grading Standards: CGC Specs
Required Quantity: [REQUIRED_QUANTITY]
Task: I need 44000 bushels with 9% moisture
Instructions:
1. Basic Validation
If Required Quantity is missing or ≤ 0 → return error
If Bin Data is empty or invalid → return error
If Task is not related to grain withdrawal/grading → return error
2. Extract Constraints (Dynamic)
From I need 44000 bushels with 9% moisture, extract:
Required quantity (if mentioned)
Any grading conditions (dynamic), e.g.:
“under 12%”
“damage < 5%”
“foreign material ≤ 2%”
Match these conditions with available grading factors in Bin Data
Apply them as strict filters
Ignore unknown factors not present in Bin Data
3. Filter Bins
Keep only bins that satisfy ALL extracted conditions
If no bins match → return error
4. Quality Score (0–100)
Apply only on filtered bins
Lower score = poorer quality
Penalize worse values across all available grading factors
5. Sort
Sort bins in ascending order (lowest score first)
If tie → prioritize worse grading factors
6. Withdrawal
Withdraw sequentially from sorted bins
Take min(available, required remaining)
Stop immediately when requirement is met
Do NOT over-withdraw
Do NOT include unused bins
7. Edge Case
If filtered inventory < required quantity:
Withdraw all filtered bins
Mention shortage in summary
OUTPUT RULES
Return ONLY ONE valid JSON
No extra text, no markdown
Quantities must be numbers
Output Format

Success:
{
"withdrawals": [
{ "id": "Bin Id", "name": "Bin Name", "qty": number }
],
"html_summary": "

Explain filtering (dynamic factors) + withdrawal briefly.

"
}

Error:
{
"withdrawals": [],
"html_summary": "

Error: Reason for failure.

"
}"