---
name: llm_completion_retry
kind: function
lang: py
domain: core
version: "1.0.0"
purity: impure
signature: "def llm_completion_retry(model: str, prompt: str, chat_history: list = None, return_finish_reason: bool = False, max_retries: int = 10, temperature: float = 0) -> str"
description: "Completion LLM sincrono con retry automatico (max 10). Soporte multi-modelo via litellm (OpenAI, Anthropic, etc.)."
tags: [llm, completion, retry, litellm, api, pendiente-usar]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: "error_go_core"
imports: [litellm, logging, time]
params:
  - name: model
    desc: "Nombre del modelo LLM soportado por litellm"
  - name: prompt
    desc: "Prompt del usuario a procesar"
  - name: chat_history
    desc: "Historial de chat opcional con formato [{'role': '...', 'content': '...'}]"
  - name: return_finish_reason
    desc: "Si True, retorna tupla (content, finish_reason) en lugar de solo content"
  - name: max_retries
    desc: "Número máximo de reintentos ante fallos"
  - name: temperature
    desc: "Temperatura para la generación"
output: "String con la respuesta del modelo, o tupla (str, str) si return_finish_reason=True"
tested: false
tests: []
test_file_path: ""
file_path: "python/functions/core/llm_completion_retry.py"
source_repo: "https://github.com/VectifyAI/PageIndex"
source_license: "MIT"
source_file: "pageindex/utils.py"
---

## Ejemplo

```python
response = llm_completion_retry("gpt-4o", "Explain quantum computing in one sentence")
# "Quantum computing uses quantum bits..."

# Con historial de chat
history = [{"role": "user", "content": "Hi"}, {"role": "assistant", "content": "Hello!"}]
response = llm_completion_retry("claude-sonnet-4-20250514", "What's 2+2?", chat_history=history)

# Con finish reason
content, reason = llm_completion_retry("gpt-4o", "...", return_finish_reason=True)
# reason: "finished" | "max_output_reached" | "error"
```

## Notas

Requiere `pip install litellm`. Soporta 100+ modelos via litellm. Retry con sleep(1) entre intentos. Retorna string vacio si todos los intentos fallan.
