Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save quantra-go-algo/52564f5300ccdf7b794ea805ff388e9b to your computer and use it in GitHub Desktop.

Select an option

Save quantra-go-algo/52564f5300ccdf7b794ea805ff388e9b to your computer and use it in GitHub Desktop.
guardrailed-llm-agent — Step 5: The LLM Risk Manager: Building the Policy Table (snippet 3)
# Retry once on parse failure, fall back to rule policy if both fail
for attempt in range(2):
content = deepseek_chat(messages, max_tokens=2000)
try:
obj = parse_json(content)
return obj.get('policy', {})
except ValueError as e:
if attempt == 1:
print(f'Parse failed twice for {month_start.date()}, using rule fallback')
return rule_policy(stats) # deterministic fallback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment