#+TITLE: SKILL: OpenRouter Provider Agent (Unified Backend) #+ID: skill-provider-openrouter-agent #+STARTUP: content * Overview The **OpenRouter Provider Agent** acts as a unified gateway to hundreds of large language models. By integrating the OpenRouter API, it provides the Neurosymbolic Kernel with unprecedented flexibility, allowing it to dynamically switch between proprietary and open-source models based on the required "tier" of intelligence. * The Unified Mandate 1. **Abstraction:** Provide an OpenAI-compatible interface that can reach any model hosted on OpenRouter. 2. **Dynamic Routing:** Support tiered model selection (Powerful vs. Fast vs. Free) to optimize for both latency and cognitive depth. 3. **Resilience:** Leverage OpenRouter's auto-routing capabilities as a fallback mechanism. 4. **Transparency:** Correct identify the agent to the provider for proper rate-limiting and metadata tracking. * Symbolic Implementation (The Logic) The implementation focuses on the flexible resolution of model IDs and the authenticated request cycle. ** Architectural Intent: OpenRouter Request Execution This function negotiates the model selection by checking for explicit model overrides in the config or falling back to a tiered default. it then executes the completion request using the OpenRouter-specific headers (Referer and Title) to ensure compliant API usage. #+begin_src lisp (defun execute-openrouter-request (prompt system-prompt) "Executes a completion request via the OpenRouter API (OpenAI-compatible)." (let ((api-key (org-agent::get-env "OPENROUTER_API_KEY")) (config-pkg (find-package :org-agent.skills.skill-environment-config))) (unless api-key (return-from execute-openrouter-request "(:type :LOG :payload (:text \"OpenRouter key missing\"))")) (let* ((get-config-fn (when config-pkg (find-symbol "GET-CONFIG-ATTRIBUTE" config-pkg))) (get-tiered-fn (when config-pkg (find-symbol "GET-TIERED-MODEL" config-pkg))) ;; Try to find a specific OpenRouter model, or a generic tiered model (model (cond ((and get-config-fn (funcall get-config-fn :LLM_MODEL_OPENROUTER nil)) (funcall get-config-fn :LLM_MODEL_OPENROUTER nil)) ((and get-tiered-fn (funcall get-tiered-fn :fast nil)) (funcall get-tiered-fn :fast nil)) (t "meta-llama/llama-3-70b-instruct"))) (url "https://openrouter.ai/api/v1/chat/completions") (body (cl-json:encode-json-to-string `((model . ,model) (messages . (((role . "system") (content . ,system-prompt)) ((role . "user") (content . ,prompt)))))))) (handler-case (let* ((response (dex:post url :headers `(("Content-Type" . "application/json") ("Authorization" . ,(format nil "Bearer ~a" api-key)) ("HTTP-Referer" . "https://github.com/org-agent/org-agent") ("X-Title" . "org-agent")) :content body)) (json (cl-json:decode-json-from-string response))) ;; Extract content from OpenAI-compatible response structure (cdr (assoc :content (cdr (assoc :message (car (cdr (assoc :choices json)))))))) (error (c) (format nil "(:type :LOG :payload (:text \"OpenRouter Failure (~a) - ~a\"))" model c)))))) ;; Register the backend (org-agent:register-neuro-backend :openrouter #'execute-openrouter-request) #+end_src ** Architectural Intent: Model Discovery Provides a curated list of top models across different price and performance tiers, enabling the system to introspect its diverse range of available brains. #+begin_src lisp (defun get-available-models () "Returns a curated list of top LLM models supported by OpenRouter, including free tiers." '((:id "moonshotai/kimi-k2.5" :context "128k" :tier :powerful) (:id "anthropic/claude-3.5-sonnet" :context "200k" :tier :powerful) (:id "google/gemini-flash-1.5" :context "1m" :tier :fast) (:id "google/gemma-2-9b-it:free" :context "8k" :tier :free) (:id "mistralai/pixtral-12b:free" :context "32k" :tier :free) (:id "openrouter/auto" :context "varying" :tier :free))) #+end_src * Registration #+begin_src lisp (defskill :skill-provider-openrouter :priority 100 :trigger (lambda (context) nil) :neuro (lambda (context) nil) :symbolic (lambda (action context) action)) #+end_src