$npx -y skills add tranhieutt/software_development_department --skill llm-app-patternsProvides architectural patterns for LLM-powered applications and AI assistants, including prompt engineering, RAG, agent loops, conversation management, and evaluation. Use when building AI-based features, chatbots, or complex AI system architectures.
| 1 | # LLM Application & AI Assistant Patterns |
| 2 | |
| 3 | ## Resources |
| 4 | |
| 5 | |
| 6 | |
| 7 | ## Architecture decision matrix |
| 8 | |
| 9 | | Pattern | Use when | Cost | |
| 10 | |---|---|---| |
| 11 | | Simple RAG | FAQ, docs Q&A | Low | |
| 12 | | Hybrid RAG (semantic + BM25) | Mixed query types | Medium | |
| 13 | | Function calling | Structured tool use | Low | |
| 14 | | ReAct agent | Multi-step reasoning | Medium | |
| 15 | | Plan-and-execute | Complex decomposable tasks | High | |
| 16 | | Multi-agent | Research, critique-refine | Very High | |
| 17 | |
| 18 | ## RAG: critical config numbers |
| 19 | |
| 20 | ```python |
| 21 | CHUNK_CONFIG = { |
| 22 | "chunk_size": 512, # tokens — sweet spot for most docs |
| 23 | "chunk_overlap": 50, # prevents context loss at boundaries |
| 24 | "separators": ["\n\n", "\n", ". ", " "], |
| 25 | } |
| 26 | # Hybrid search alpha: 1.0=semantic only, 0.0=BM25 only, 0.5=balanced |
| 27 | ``` |
| 28 | |
| 29 | ## RAG: retrieval strategies |
| 30 | |
| 31 | ```python |
| 32 | # Basic: semantic search |
| 33 | results = vector_db.similarity_search(embed(query), top_k=5) |
| 34 | |
| 35 | # Better: hybrid (semantic + keyword via RRF) |
| 36 | def hybrid_search(query, alpha=0.5): |
| 37 | return rrf_merge(vector_db.search(query), bm25_search(query), alpha) |
| 38 | |
| 39 | # Best for recall: multi-query (3 variations, deduplicate) |
| 40 | queries = llm.generate_variations(query, n=3) |
| 41 | results = deduplicate([semantic_search(q) for q in queries]) |
| 42 | ``` |
| 43 | |
| 44 | ## RAG: generation prompt template |
| 45 | |
| 46 | ```python |
| 47 | RAG_PROMPT = """Answer based ONLY on the context below. |
| 48 | If insufficient, say "I don't have enough information." |
| 49 | |
| 50 | Context: {context} |
| 51 | Question: {question} |
| 52 | Answer:""" |
| 53 | ``` |
| 54 | |
| 55 | ## Agent: function calling loop |
| 56 | |
| 57 | ```python |
| 58 | messages = [{"role": "user", "content": question}] |
| 59 | while True: |
| 60 | response = llm.chat(messages=messages, tools=TOOLS, tool_choice="auto") |
| 61 | if not response.tool_calls: |
| 62 | return response.content |
| 63 | for call in response.tool_calls: |
| 64 | result = execute_tool(call.name, call.arguments) |
| 65 | messages.append({"role": "tool", "tool_call_id": call.id, "content": str(result)}) |
| 66 | ``` |
| 67 | |
| 68 | ## Production: caching (only temperature=0 responses) |
| 69 | |
| 70 | ```python |
| 71 | def get_or_generate(prompt, model, **kwargs): |
| 72 | deterministic = kwargs.get("temperature", 1.0) == 0 |
| 73 | if deterministic: |
| 74 | key = sha256(f"{model}:{prompt}:{json.dumps(kwargs, sort_keys=True)}") |
| 75 | if cached := redis.get(key): return cached |
| 76 | response = llm.generate(prompt, model=model, **kwargs) |
| 77 | if deterministic: redis.setex(key, 3600, response) |
| 78 | return response |
| 79 | ``` |
| 80 | |
| 81 | ## Production: retry + fallback |
| 82 | |
| 83 | ```python |
| 84 | from tenacity import retry, wait_exponential, stop_after_attempt |
| 85 | |
| 86 | @retry(wait=wait_exponential(multiplier=1, min=4, max=60), stop=stop_after_attempt(5)) |
| 87 | def call_llm(prompt): return llm.generate(prompt) |
| 88 | |
| 89 | # Fallback chain |
| 90 | for model in [primary] + fallbacks: |
| 91 | try: return llm.generate(prompt, model=model) |
| 92 | except (RateLimitError, APIError): continue |
| 93 | ``` |
| 94 | |
| 95 | ## LLMOps: key metrics |
| 96 | |
| 97 | ``` |
| 98 | Latency : p50, p99 response time |
| 99 | Quality : satisfaction (thumbs), task completion %, hallucination rate |
| 100 | Cost : cost_per_request, tokens_per_request, cache_hit_rate |
| 101 | Health : error_rate, timeout_rate, retry_rate |
| 102 | ``` |
| 103 | |
| 104 | ## Embedding model selection |
| 105 | |
| 106 | | Model | Dims | Cost | Use | |
| 107 | |---|---|---|---| |
| 108 | | text-embedding-3-small | 1536 | $0.02/1M | Most cases | |
| 109 | | text-embedding-3-large | 3072 | $0.13/1M | High accuracy | |
| 110 | | bge-large (local) | 1024 | Free | Self-hosted | |