Is there a cache for XQuery code? I work with small snippets and can
most of the time choose if something is a literal or passed as an input
variable.

If you want to resort to the string representation of a query, you could store it in the cache as well, and evaluate it later on, e.g. as follows:

  'name-of-query'
  => cache:get()
  => xquery:eval()

Query strings could also be organized in an XQuery map:

  (: run cached query :)
  let $map := cache:get('queries')
  let $query := $map?name-of-query
  return xquery:eval($query)

  (: register query :)
  '123 + 456'
  => cache:get('queries')
  => map:put('name-of-query', $query)
  => cache:put()